MATLAB Answers

GUI MATLAB- Loading Multiple file.txt and saving them in separate variable

2 views (last 30 days)
Laura Lippi
Laura Lippi on 21 May 2015
Answered: Jan on 21 May 2015
I'm doing a GUI with Matlab that should load multiple file.txt from the same folder pressing a button. After this step I would like to save every file with different variable name. I try in two different ways, without results
First way --> In this way, I tried to do a multiple file selection, but I can't save my file with different variable name
[FileName_FM,PathName_FM,FilterIndex_FM] = uigetfile('*.txt*','MultiSelect','on');
numfiles = size(FileName_FM,2);
for ii = 1:numfiles
FileName_FM{ii}
entirefile =fullfile(PathName_FM,FileName_FM{ii})
fid = fopen(entirefile);
DataFM=importdata(entirefile);
Data_FM=getfield(DataFM,'data');
fclose(fid);
end
  • * Second way * * --> In this way I tried to load all file but I don't know why, it doesn't work
[filename_FM,pathname_FM] = uigetfile({'*.txt'},'Pick a txt file');
filepath_FM=fullfile(pathname_FM,filename_FM);
DataFM1= importdata(filepath_FM);
Data_FM1=getfield(DataFM1,'data');
Ax_FMq1=Data_FM1(:,1)
Ay_FMq1=Data_FM1(:,3);
Az_FMq1=Data_FM1(:,4);
[filename_FM,pathname_FM] = uigetfile({'*.txt'},'Pick a txt file');
filepath_FM=fullfile(pathname_FM,filename_FM);
DataFM2= importdata(filepath_FM);
Data_FM2=getfield(DataFM2,'data');
Ax_FMq2=Data_FM2(:,2);
Ay_FMq2=Data_FM2(:,3);
Az_FMq2=Data_FM2(:,4);
In this case Matlab get this error:
Attempted to access Data_FM1(:,3); index out of bounds because numel(Data_FM1)=1.
Error in GUI_ANALISI>pushbutton1_Callback (line 138)
Ax_FMq1=Data_FM1(:,3)
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in GUI_ANALISI (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)GUI_ANALISI('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Thank you very much for your help!

Answers (1)

Jan
Jan on 21 May 2015
I do not see any approach to save the files in the first piece of code. How do you try to save the files under a different name? Is the import of the files contents useful for anything?
I'd expect a simple copyfile(OriginalName, NewName) should be sufficient without an import of the data.
The error message for the 2nd part of the code is clear: You try to access Data_FM1 as an array, but it is a scalar. So use the debugger to find out, what this variable is. Set a breakpoint in this line and check the class and size of this variable. But again: according to your question, an import is not required.

Community Treasure Hunt

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

Start Hunting!