1

I am using a batch file:

@echo off
C:\Octave\Octave-4.4.1\octave.vbs --force-gui --eval batchTest("'%~dp0'")
cmd /c

to run an Octave script

function [] = batchTest(fPath)

disp(fPath);
cd(fPath);

optionNumber = input('Choose option 1 or 2:  ');

if optionNumber == 1            
    fName = input('Input file description:  ',"s");
    filename = [fName ".xlsx"];
    xls = xlsopen(filename,1);       % <-- THIS DOES NOT WORK, PRODUCES "FILE POINTER PRESERVED MESSAGE"
    xls = oct2xls({"OutputData"},xls,1,"A1");
    xlsclose(xls);
end

if optionNumber == 2            
    filename = "TestFile.xlsx";
    xls = xlsopen(filename,1);           %  <-- THIS WORKS AS EXPECTED
    xls = oct2xls({"OutputData"},xls,1,"A1");
    xlsclose(xls);
end

to create an Excel file in the batch file's directory.

Option number 1 produces a "File pointer preserved" warning, and the Excel file is not created. It seems that I can't use any string that was created, in whole or in part, by Octave's 'input' function. Inputting the full filename with ".xlsx" and passing that variable to the 'xlsopen' function does not help. Option 2 works fine, but I need to produce multiple files, so the "fName" descriptor is important. I've tried adding SETLOCAL ENABLEDELAYEDEXPANSION to the batch file. I've also tried a work-around where I used Option 2, and then added

rename("TestFile.xlsx",[fName ".xlsx"])

to the Octave script, but this produces an "invalid input" error in the 'rename' function, so it doesn't like the 'input'-created string either. The problem is only with the 'xlsopen' and 'rename' functions; the 'input' function works just fine for choosing the option number.

Either option works when directly executing 'batchTest(pwd)' from the Octave command line. The issue only arises when executing from the batch file. Any advice would be much appreciated.

6
  • 1
    "this does not work": well of course it doesn't. You overwrite the xls filehandle with an oct2xls object. It's no longer the same filehandle. I'm more surprised the second "works" instead. It would REALLY help if you showed us your output instead of just saying "it doesn't work" though. Does your .bat file pass the intended path correctly? As a string? With the correct file separators? Are you passing a unix filepath? Windows? With valid drive? Just the filename? Does the resulting filename point to the intended fail with a valid pathstring? Does the (first) xls object open normally? Commented Apr 24, 2021 at 21:27
  • if I had to guess, I'd be guessing that your filename is not what you think it is, and that octave is interpreting backslashes in the path as escape sequences. You should not realy on typing paths manually like that, aim to use fullfile instead. Commented Apr 24, 2021 at 21:30
  • @Tasos Papastylianou, I appreciate the tip on fullfile, though I'm only using the pathname to change the current working directory, and so it doesn't appear in the xlsopen function, only the filename. My problem seems to arise even before the excel file issue, since when I execute f1 = "String1"; f2 = input('Input file description: ',"s"); (::type String2::) fExt = ".xlsx"; filename = [f1 f2 fExt]; disp(filename) strncmp(filename,f1,length(f1)) This appears: .xlsxg1String2. However, the strncmp output is 1, so it only seems to display improperly, not actually be in that form. Commented Apr 25, 2021 at 16:08
  • @smcmi right, this confirms my suspicions. When you collect f2 via input, you are accidentally attaching a \r character to your input, which corresponds to the 'carriage return' character. Your string does not contain what you think it does. type double(f2) to see you have an extra character at the end (which presumably will be decimal number 13, the ascii number for the \r character). If you use strtrim on your strings before concatenating them, it should work (assuming there's no other silly stuff going on). Commented Apr 25, 2021 at 16:43
  • 1
    @TasosPapastylianou, you were exactly right in every point you made in your last comment. The strtrim function removed the carriage return, and everything is working perfectly now. My sincerest thanks! Can I mark your comment as an accepted answer? Commented Apr 25, 2021 at 18:53

1 Answer 1

3

The issue sounds to be that when you create your string, you are including escaped characters that mess up your filename.

From the discussion in the comments it seems the carriage return character is being included in your string, resulting in a wrong filename.

It is unclear why this is only the case when running from the batch file, but as a workaround, you can ensure that the carriage return is removed by preprocessing your string input with strtrim to remove any unwanted whitespace.

Sign up to request clarification or add additional context in comments.

1 Comment

Dear @smcmi, since it is still not clear why the carriage return makes it to the string when running in batch mode but not in the interpreter, I have submitted a bug report: Bug #60453. If you don't mind, please do contribute to that discussion too if necessary :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.