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.
xlsfilehandle with anoct2xlsobject. 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 resultingfilenamepoint to the intended fail with a valid pathstring? Does the (first) xls object open normally?fullfileinstead.fullfile, though I'm only using the pathname to change the current working directory, and so it doesn't appear in thexlsopenfunction, only the filename. My problem seems to arise even before the excel file issue, since when I executef1 = "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.\rcharacter to your input, which corresponds to the 'carriage return' character. Your string does not contain what you think it does. typedouble(f2)to see you have an extra character at the end (which presumably will be decimal number 13, the ascii number for the\rcharacter). If you usestrtrimon your strings before concatenating them, it should work (assuming there's no other silly stuff going on).strtrimfunction removed the carriage return, and everything is working perfectly now. My sincerest thanks! Can I mark your comment as an accepted answer?