Alexander Wilkens
Alexander Wilkens

Reputation: 13

Octave: Problems with load

I'm currently doing a program in Octave where I want the user to be able to insert the file that he wants to load. The files in question are .mat files and are loaded with

load ("filename.mat")

I was thinking about doing something like this:

file=input("Whats the file name: ")

load ("file")

But that didn't work...

Anyone got any tips?

Upvotes: 0

Views: 251

Answers (2)

Alexander Wilkens
Alexander Wilkens

Reputation: 13

I used Benoit_11's method but changed it to input instead since inputdlg doesn't seem to work in Octave.

clear

clc

name=input('Enter the file name, without the file extension: ','s')

filename = strcat(name,'.mat')

S = load(filename)

Upvotes: 1

Benoit_11
Benoit_11

Reputation: 13945

That's likely because you need to input the file name enclosed in single quotation marks : 'filename'. (Note: I use MATLAB but that should work just the same in Octave).

As an alternative you can use inputdlg to request user input. It gives you much flexibility as you can add fields to the prompt such as the file extension or else.

Here is a simple example:

clear
clc


prompt = {'Enter file name'};
dlg_title = 'Input';
num_lines = 1;
def = {'Dummy file'};
answer = inputdlg(prompt,dlg_title,num_lines,def)

The prompt looks like this:

enter image description here

You can fetch the asnwer like so:

name = answer{1};

And finally add the extension to load the .mat file:

filename = strcat(name,'.mat')
S = load(filename)

To do it in one go with the file extension:

prompt = {'Enter file name'; 'Enter file extension'};
dlg_title = 'Input';
num_lines = 1;
def = {'Dummy file'; '.mat'};
answer = inputdlg(prompt,dlg_title,num_lines,def)

name = answer{1};
extension = answer{2};

filename = strcat(name,extension)
S = load(filename)

Hope that helps!

Upvotes: 2

Related Questions