简体   繁体   中英

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?

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:

在此处输入图片说明

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!

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM