Load multiple data and put in a list box with app designer

Hello all,
I want to load multiple files using a button, and display the filenames in a list box, using app designer. Further in the GUI, I want to be able to recall specific files in this list box to use their data for plotting or calculating for example.
To do this, I use a button called 'Load Files'. This button says:
filename = uigetfile('*.m','Select the MATLAB code files', 'MultiSelect', 'on');
varlist = who('-file',filename);
app.DataOutput.Items = varlist(1);
The error I get here is 'Error using who. Argument must contain a string'. What's going wrong here?
EDITED (11/9/2017): What I have now is: (where app.DataOutput is the listbox)
filename = uigetfile('*.m','Select the MATLAB code files', 'MultiSelect', 'on');
for i = 1:length(filename)
varlist = who('-file',filename{i})
app.DataOutput.Items(i) = varlist(1)
end
It now puts all the files in the list box. But the problem here is that it is not loading the data of these files in the workspace. (I need the data from these files to be called later in the GUI to do calculations and plots with)
The next step is to only use the data from the selected files in the list box. So, let's say I have loaded six files in the list box. I first plot all these data in a figure which gives me six line graphs in one UIaxes. After seeing these plots, I'm only interested in the first three plots. So I select the first three filenames in the list box (and press a button) so only the data of these files are being used for plotting now.
Is it possible, for example, to have a command in app designer that loads data from a chosen folder in the workspace under a name that is given in a loop? For example, I load six files in app designer, using uigetfile. These files have different names, let's say: 'vibrationdata01', 'vibrationdata02', 'shockdata01', 'shockdata02', 'initialstate' and 'endstate'. If I could give these data a variable name, it would be easier to call these data later in the app designer. Easiest would be: a = 'filename 1'..... n = 'filename n'.
Is this possible to do with app designer?

4 commentaires

Did you select a filename? It seems filename is not a string, which would occur if you click 'Cancel' on the uigetfile dilog and maybe other circumstances.
As with many many similar questions, the simple answer is just put a breakpoint in or use the pause/stop on errors functionality and look at the variable the error message is pointing to.
acegi bdegi
acegi bdegi le 9 Nov 2017
Modifié(e) : acegi bdegi le 9 Nov 2017
Yes, I select two files, named data_01 and data_02. (but the names of the files shouldn't matter, as I want to use this GUI for every file that I want to load data from).
With loading only one file it's going right: it displays the name in the list box. If I select two files, it gives me the error, described above.
I realize that 'data_01' contains both a string and a number.
EDITED: I updated the main thread. It does now shows the files in the list box. I don't know what I did wrong, but this part is working now.
for i = 1:length(filename)
varlist = who('-file',filename{1})
app.DataOutput.Items(i) = varlist(1)
end
Not sure how this is working for you, you're repeatedly inspecting the first file.
As for getting to the actual data (not just filenames and variable names), did you try the load function?
That's a mistake, I meant filename{i}. Changed in topic, tnx for noticing.
I have tried the command
uiopen('load')
But the problem here is that it doesn't give me the opportunity to open multiple files and
x = uiopen('load')
is not a valid command, so I cannot call specific files later in my GUI, having a random filename. If I just use uiopen('load'), (which opens the folder as well) and select a .mat file which contains data, it doesn't load the data in my MATLAB workspace, but instead, it saves it somewhere I don't know. If I plot the data, using the known names for the data, it plots in the graph very well.
uiopen('load')
plot(app.TimeGraph, data_01_x, data_01_y)
But I don't want to use the data names in my code view, because I want to load different data each time. It has to be a variable.

Connectez-vous pour commenter.

Réponses (1)

% You can use the MATLAB's datastore function instead of using the who function
ds=datastore("Directory Path");
% Use a for loop and read all the files
files={}; % Initialize a null cell array
for i=1:1:length(ds.Files)
files{i}=read(ds);
end

Catégories

En savoir plus sur Environment and Settings dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by