Clear Filters
Clear Filters

Loading a certain column from an ascii file to a list box in gui matlab

3 views (last 30 days)
Hello,
I am trying to load the content of a certain column in an ascii file where the column number is given by an edit box like that:
[filename pathname] = uigetfile({'*.dat;*.txt','ASCII Files';'*.*','All Files' },'Look for MELT data','MultiSelect','on');
fullpathname=strcat(pathname , filename);
x = fileread(fullpathname);
tScan = textscan(x, '%s %f %s','headerlines',1);
newScan = tScan{:};
col=getappdata(0,'edit16');% to read the entered column in the edit box
G=newScan(:,col);
set(handles.edit18,'string',G);
The problem is: if I entered the column number (col) as 1 it gives the correct content. But if entered any other number for (col) I get the following error
Index exceeds matrix dimensions.
Error in gui3>pushbutton8_Callback (line 406)
G=newScan(:,col);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in gui3 (line 46)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)gui3('pushbutton8_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
Could someone help me?

Accepted Answer

Jan
Jan on 8 Jan 2019
It is a bad idea to store variables globally:
col=getappdata(0,'edit16')
If you open several instances of the GUI, they overwrite the application data of the root object. This suffers from the same horror as using global. I guess, that this is much easier and safer:
col = get(handles.edit16, 'String')
We cannot know, where the value of getappdata(0,'edit16') has been set to which value. Even for you it is not trivial to find out, where and when this value was set. But you can use at least the debugger to find the value:
dbstop if error
Now Matlab stops, when an error occurs and you can check the value of col.
  8 Comments
Ahmed Elsherif
Ahmed Elsherif on 9 Jan 2019
Edited: Ahmed Elsherif on 9 Jan 2019
Thanks a lot. But the selection of the desired column is random as it may take any value not only the second column. Your suggestion works perfect for
set(handles.edit_tauexp,'string',data);
in the following (complete) code
% tScan = textscan(x, '%s %f %s','headerlines',1);
tScan = textscan(x, '%s%f%s%*[^\n]', 'HeaderLines', 1);
newScan = tScan{:,:};
data=newScan;
set(handles.edit_tauexp,'string',data);
% another section to update from an edit box
data1 =get(handles.edit_T,'String');
data1 = cellstr(data1);
set(handles.edit_Texp,'string',repmat(data1,1379,1));
% third section to look for a certain column index and print its content in a list box
col=getappdata(0,'edit16');
G=newScan(:,col);
set(handles.edit18,'string',G);
but for the last line the code it shows the error
Index exceeds matrix dimensions.
Error in gui3>pushbutton8_Callback (line 423)
G=newScan(:,col);
Even before using your suggestion and using the commented first line!
Walter Roberson
Walter Roberson on 9 Jan 2019
tScan = textscan(x, '%s%f%s%*[^\n]', 'HeaderLines', 1);
newScan = tScan{:,:};
Don't do that. The result of textscan() is a cell array with one entry for each item in the format specification that is not "*" . So in the above, %s%f%s is three non-* format items, so the output of textscan would be a 1 x 3 cell array. When you then use {:,:} on that and assign to a single output location, if you do not outright get an error, then newScan would get assigned the first of the results, equivalent to as if you had done
newScan = tScan{1,1};
which would be a cell column array of character vectors, one entry for the first field of each line that was parsed.
It would be possible to create an N x 3 cell array in which each row corresponded to one input line, with the columns corresponding to entries. To do that, you need to use
newScan = [tScan{1}, num2cell(tScan{2}), tScan{3}];

Sign in to comment.

More Answers (1)

Ahmed Elsherif
Ahmed Elsherif on 9 Jan 2019
Dears Jan and Walter, I solved the problem. I was using 'data' while with using the original 'tScan', it works fine.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!