smcmi
smcmi

Reputation: 98

Octave 'input' function, run through batch file

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.

Upvotes: 1

Views: 323

Answers (1)

Tasos Papastylianou
Tasos Papastylianou

Reputation: 22215

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.

Upvotes: 3

Related Questions