You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Why importing ascii file and doing differentiation on it by GUI MATLAB shows this error 'Undefined operator '<=' for input arguments of type 'cell'.' ?
1 view (last 30 days)
Show older comments
Dears,
I come to my first Matlab code and I have no previous experience with Matlab so I need your help. I wrote the following code in order to load an ascii file of different rows and columns by push_callback in GUI Matlab. Before loading the file I entered value to define the column of interest and I loaded the desired 'tauD'. Afterwards, I need this code to do some differentiations, save the results, and finally to draw the ouput. But, I got the following message
'
' Undefined operator '<=' for input arguments of type 'cell'.
Error in PSD>pushbutton1_Callback (line 128)
I=imp(tauD(1,1)<=imp(:,1)&imp(:,1)<=tauD(end,1),col+1);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in PSD (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)PSD('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback''
So, could you please help me to fix this errors?
Your efforts and time are deeply appreciated in advance!
tauD_file='myOptions';
col=getappdata(0,'edit1');
load(tauD_file);
tauD=myOptions;
[fnames,fpath]=uigetfile({'*.dat;*.txt','ASCII Files';'*.*','All Files' },'Select data file','MultiSelect','on');
if ~iscell(fnames)
fnames={fnames};
end
for k=fnames
X1 = cell2mat(k)
imp=importdata([fpath,char(X1)]);
I=imp(tauD(1,1)<=imp(:,1)&imp(:,1)<=tauD(end,1),col+1);
[~,fname]=fileparts(char(X1));
dtau=gradient(tauD(:,1));
dtaudD=gradient(tauD(:,1),tauD(:,2));
dtaudlogD=gradient(tauD(:,1),log(tauD(:,2)));
dVdD=I.*dtaudD./dtau;
dVdlogD=I.*dtaudlogD./dtau;
result=[tauD(:,2),dVdD,dVdlogD];
save([fpath,'dVdD(C8)_cyl_0.193_6_',fname,'.dat'],'result','-ascii')
end
set(handles.pushbutton1,'String',filename);
a = get (handles.pushbutton1,'String',dVdlogD(:,1));
b= get (handles.pushbutton1,'String',dVdlogD(:,3));
axes(handles.axes1);
plot(eval(a), eval(b))
1 Comment
Answers (1)
Ahmed Elsherif
on 30 Nov 2018
@ Walter Robers
Should I change it by cell2mat or what?
23 Comments
Walter Roberson
on 30 Nov 2018
What data type and size is MyOptions ? What is its meaning relative to
I=imp(tauD(1,1)<=imp(:,1)&imp(:,1)<=tauD(end,1),col+1);
?
Is MyOptions a cell array in which the first element holds a numeric scalar that represents a lower bound, and the last element holds a numeric scalar that represents an upper bound?
I=imp(tauD{1,1}<=imp(:,1)&imp(:,1)<=tauD{end,1},col+1);
perhaps.
Ahmed Elsherif
on 3 Dec 2018
Edited: Ahmed Elsherif
on 3 Dec 2018
It is a cell array. I defined it in a pop up menu callback later in the code.
set(handles.popupmenu2,'String',{'Spherical&Delta=0.18nm', 'Cylindrical&Delta=0.18nm','Spherical&Delta=0.193nm','Cylindrical&Delta=0.193nm'});
% create the cell array of string options
myOptions = {'Select','tauD_sph_T_300K_delta_0_18nm', 'tauD_cyl_T_300K_delta_0_18nm','tauD_sph_T_300K_delta_0_193nm','tauD_cyl_T_300K_delta_0_18nm'};
% save to a mat file
save('myOptions.mat','myOptions');
I've got this structure from a script decribed in the internet for calling the pop up menu.
Walter Roberson
on 3 Dec 2018
it is aa cell array of character vectors . you are trying to use <= between parts of it and what appears to be a numeric array . What result are you expecting when you compare 'Select' to imp() ?
Ahmed Elsherif
on 3 Dec 2018
Edited: Ahmed Elsherif
on 3 Dec 2018
Sorry, I cannot understand. I am just using imp in
I=imp(tauD(1,1)<=imp(:,1)&imp(:,1)<=tauD(end,1),col+1);
to get the whole contents of the imported file from
imp=importdata([fpath,char(k)]);
then
[~,fname]=fileparts(char(k));
Afterwards, I need to do derivations like that
dtau=gradient(tauD(:,1));
dtaudD=gradient(tauD(:,1),tauD(:,2));
dtaudlogD=gradient(tauD(:,1),log(tauD(:,2)));
dVdD=I.*dtaudD./dtau;
dVdlogD=I.*dtaudlogD./dtau;
result=[tauD(:,2),dVdD,dVdlogD];
save([fpath,'dVdD_',fname,'.dat'],'result','-ascii')
But when I run this code and after omitting the
X1 = cell2mat(k)
part, I got the following error message
Cell contents reference from a non-cell array object.
Error in PSD>pushbutton1_Callback (line 115)
I=imp(tauD(1,1)<=imp{:,1}&imp{:,1}<=tauD(end,1),col+1);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in PSD (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)PSD('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
I would be so helpful if you tell me what I should exactly change/modify.
Walter Roberson
on 3 Dec 2018
mask1 = bsxfun(@le, tauD{1,1}, imp(:,1));
n1 = size(mask1 ,2);
mask2 = bsxfun(@ge, tauD{end,1}, imp(:,1));
n2 = size(mask2 ,2);
n = min(n1,n2);
mask3 = mask1(:,1:n) & mask2(:,1:n);
temp = repmat(imp(:,col+1),n,1).';
I = temp(mask3.');
This is almost certainly going to be empty . tauD is a cell array that is aa row vector . Because it is aa row vector accessing (1,1) and (end,1) are the same location so you are testing that the imp values are less than or equal to and greater than or equal to aa value that is the same on both sides .The test can only work if the imp values exactly equal tauD(1;1)
So what value is tauD(1,1) ? With tauD having been assigned MyOptions it is going to be the first entry in MyOptions which is the character vector 'Select' . So you are testing the imp values to see if they exactly equal 'S' and 'e' and 'l' and so on. 'S' is numeric 83 and 'e' is numeric 101 and so on. It is not impossible that you have values that aree exactly 83 or 101 but probably not.
Ahmed Elsherif
on 4 Dec 2018
Edited: Ahmed Elsherif
on 4 Dec 2018
Dear Walter, thanks a lot. it works fine. Just I had to change the curly brackets into parentheses. I still have a problem. The 2nd column of tauD, i.e. tauD(:,2) has at the beginning some zeros which made the 2nd and 3rd columns of the output matrix (result=[tauD(:,2),dVdD,dVdlogD]) are NAN all the time. How to do gradient like (dtaudD=gradient(tauD(:,1),tauD(:,2));) while tauD(:,2) has some zeros? How to fix this?
Warm thanks for you.
Walter Roberson
on 4 Dec 2018
"Just I had to change the curly brackets into parentheses."
No, I can guarantee that would not have worked with what you have been describing to us.
You posted for us that
myOptions = {'Select','tauD_sph_T_300K_delta_0_18nm', 'tauD_cyl_T_300K_delta_0_18nm','tauD_sph_T_300K_delta_0_193nm','tauD_cyl_T_300K_delta_0_18nm'};
and
tauD=myOptions;
so tauD is a cell array that contains character vectors such as 'Select'. When you use () instead of {} to index tauD then you would get 1 x 1 cell arrays, and you cannot use <= or >= with cell arrays.
The second column of tauD does not have any zeros: it contains only the character vector 'tauD_sph_T_300K_delta_0_18nm'
Ahmed Elsherif
on 5 Dec 2018
Edited: Ahmed Elsherif
on 5 Dec 2018
Dear Walter,
I had to do that because with using your comment with mask1 and mask2, and running the code it gives me an error that
Undefined function or variable 'myOptions'.
Error in PSD>pushbutton1_Callback (line 110)
tauD= myOptions;
So I had to comment tauD=myOptions and I defined it elsewhere in the code with tauD= tauD_cyl_T_300K_delta_0_18nm; which is one selection from myOptions but without quotations. Really I do not know how to define myOptions in quotations with {} then to use it without both. Could you help me on that?
Ahmed Elsherif
on 11 Dec 2018
Dear Walter, thanks for your help but I still need your assistance. I followed your thoughts and the following code is working but the output file 'result' is empty and it has only one word 'NAN' in the second column.
data = load('myOptions.mat','myOptions');
set(handles.popupmenu,'String',data.myOptions);
tauD_file='myOptions';
col=getappdata(0,'edit1');
tauD=[];
load(tauD_file);
myOptions1=str2double(myOptions);
tauD= myOptions1;
[fnames,fpath]=uigetfile({'*.dat;*.txt','ASCII Files';'*.*','All Files' },'Select data file','MultiSelect','on');
if ~iscell(fnames)
fnames={fnames};
end
for k=fnames
disp(k{:})
imp=importdata([fpath,char(k)]);
mask1 = bsxfun(@le, tauD(1,1), imp(:,1));
n1 = size(mask1 ,2);
mask2 = bsxfun(@ge, tauD(end,1), imp(:,1));
n2 = size(mask2 ,2);
n = min(n1,n2);
mask3 = mask1(:,1:n) & mask2(:,1:n);
temp = repmat(imp(:,col+1),n,1).';
I = temp(mask3.');
[~,fname]=fileparts( char(k));
dtau=gradient(tauD(:,1));
dtaudD=gradient(tauD(:,1),tauD(:,2));
dtaudlogD=gradient(tauD(:,1),log(tauD(:,2)));
dVdD=I.*dtaudD./dtau;
dVdlogD=I.*dtaudlogD./dtau;
result=[tauD(:,2),dVdD,dVdlogD];
save([fpath,'output_',fname,'.dat'],'result','-ascii')
end
guidata(hObject, handles);
set(handles.pushbutton1,'String',fnames);
tauD is a matrix having 2 columns and 1369 rows. It has values from 0.05029 to 141.07922. I am trying to load my matrix using a popup menu with:
function popupmenu_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu
set(handles.popupmenu,'String',{'Spherical&Delta=0.18nm', 'Cylindrical&Delta=0.18nm','Spherical&Delta=0.193nm','Cylindrical&Delta=0.193nm'});
myOptions = {'tauD_sph_T_300K_delta_0_18nm', 'tauD_cyl_T_300K_delta_0_18nm','tauD_sph_T_300K_delta_0_193nm','tauD_cyl_T_300K_delta_0_193nm'};
save('myOptions.mat','myOptions');
All myOptions in quotations are matrix.mat. Would you please tell me what is wrong to not get results with three columns and 1369 rows in 'results'?. Thanks in advance
Walter Roberson
on 11 Dec 2018
Edited: Walter Roberson
on 11 Dec 2018
tauD is not going to be numeric if the callback has executed .The callback replaces MyOptions.mat with a .mat file that contains the single variable named MyOptions that contains a cell array of character vectors .
You load() that into data and set the popup options to the cell array of character vectors .Then you set the tauD file name to the same file name .
You then load the tauD without any output specified. So the content are put into the workspace . The file contains the one variable so after the second load MyOptions in the workspace is going to contain the cell array of character vectors .
You then str2double MyOptions which is that cell array of character vectors . Each entry in the cell will be examined as potentially containing aa single numeric value . But they do not represent numbers, they are things like 'tauD_sph_T' etc. Because they are not representation of numbers str2double converts each of them to nan.
You assign that vector of nan to a variable and assign the variable to tauD.
Therefore tauD is going to be a 1x4 vector containing nan.
Ahmed Elsherif
on 12 Dec 2018
Edited: Ahmed Elsherif
on 12 Dec 2018
Dear Walter, you are explaining what I have done but I do not know the exact problem and how to get ride of. Actually, I do not have the experience to solve this problem. I tried several methods after I google my problems but no one helped me. What I need is: Form my pushbuttun, I need to load a certain ascii files (loading works successfuly) having numerical values of different columns and 1369 rows. I tell gui to look for a certain column in this ascii file (also, works fine). Then I need the user to load my tauD options depending on the assumed model. I have predefined matrices, tauDs. Each tauD has two columns and 1369 rows, too. After having the ascii file, column number, and tauD option, I need to do the gradient
dtau=gradient(tauD(:,1));
dtaudD=gradient(tauD(:,1),tauD(:,2));
dtaudlogD=gradient(tauD(:,1),log(tauD(:,2)));
dVdD=I.*dtaudD./dtau;
dVdlogD=I.*dtaudlogD./dtau;
Then to save or draw the results.
I do not know how to read both tauD and the ascii files as numerical values to do the gradient. Would you help?
Walter Roberson
on 12 Dec 2018
When you build your GUI in GUIDE, make sure you set the String property of popupmenu to
{'Spherical&Delta=0.18nm', 'Cylindrical&Delta=0.18nm','Spherical&Delta=0.193nm','Cylindrical&Delta=0.193nm'}
then your callback should be something like
function popupmenu_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu
myOption_filenames = {'tauD_sph_T_300K_delta_0_18nm.mat', 'tauD_cyl_T_300K_delta_0_18nm.mat', 'tauD_sph_T_300K_delta_0_193nm.mat', 'tauD_cyl_T_300K_delta_0_193nm.mat'};
val = hObject.Value;
if isempty(val); return; end %no selection
chosen_file = myOption_filenames{val};
filestruct = load(chosen_file);
myOptions = filestruct.MagicVariableName;
save('myOptions.mat', 'myOptions');
This presumes that each of the .mat file is using the same variable name to store the data to be used, and that the variable name used in the .mat files is MagicVariableName .
Ahmed Elsherif
on 13 Dec 2018
Thanks a lot but it still shows the error
Reference to non-existent field 'MagicVariableName'.
Error in PSD>popupmenu_Callback (line 409)
myOptions = filestruct.MagicVariableName;
what is the problem?
After that, I wrote in the pushbuttun
tauD_file='myOptions';
tauD= myOptions;
Then I started evaluating the gradient, is it right?
Walter Roberson
on 13 Dec 2018
In the code I posted you should change MagicVariableName to TheNameOfTheVariableYourDataIsStoredInGoesHere
Ahmed Elsherif
on 13 Dec 2018
Ok and sorry again, does that mean that I have to create a file with the assumed variable name or?
-Or how to define this variable name?
Stephen23
on 13 Dec 2018
Edited: Stephen23
on 13 Dec 2018
"does that mean that I have to create a file with the assumed variable name "
It is much easier to process multiple .mat files if all of them contain exactly the same variable names. It would be simplest and most efficient if you know the name in advance, and this name is the same in all .mat files. Then you can just hardcode it as Walter Roberson showed.
Ahmed Elsherif
on 13 Dec 2018
Thanks Stephen and Walter, works very nice. But the result file shows NaN after performing
dtau=gradient(tauD(:,1));
dtaudD=gradient(tauD(:,1),tauD(:,2));
dtaudlogD=gradient(tauD(:,1),log(tauD(:,2)));
dVdD=I.*dtaudD./dtau;
dVdlogD=I.*dtaudlogD./dtau;
where
tauD=myOptions
What is wrong here?
Walter Roberson
on 13 Dec 2018
I assumed that the files to be loaded are .mat files. In order to use a variable inside a .mat file you need to know the name of the variable -- or else you need to have some way of recognizing that it is the correct variable name by examining the list of variables stored in the file. (For example it is possible to deal reasonably with the case where the .mat file has exactly one variable.)
Ahmed Elsherif
on 14 Dec 2018
Edited: Ahmed Elsherif
on 14 Dec 2018
Actually, the loaded files are numeric matrix.mat
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)