Why importing ascii file and doing differentiation on it by GUI MATLAB shows this error 'Undefined operator '<=' for input arguments of type 'cell'.' ?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Ahmed Elsherif
2018-11-30
@ Walter Robers
Should I change it by cell2mat or what?
23 个评论
Walter Roberson
2018-11-30
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
2018-12-3
编辑:Ahmed Elsherif
2018-12-3
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
2018-12-3
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
2018-12-3
编辑:Ahmed Elsherif
2018-12-3
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
2018-12-3
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
2018-12-4
编辑:Ahmed Elsherif
2018-12-4
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
2018-12-4
"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
2018-12-5
编辑:Ahmed Elsherif
2018-12-5
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
2018-12-11
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
2018-12-11
编辑:Walter Roberson
2018-12-11
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
2018-12-12
编辑:Ahmed Elsherif
2018-12-12
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
2018-12-12
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
2018-12-13
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
2018-12-13
In the code I posted you should change MagicVariableName to TheNameOfTheVariableYourDataIsStoredInGoesHere
Ahmed Elsherif
2018-12-13
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
2018-12-13
编辑:Stephen23
2018-12-13
"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
2018-12-13
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
2018-12-13
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
2018-12-14
编辑:Ahmed Elsherif
2018-12-14
Actually, the loaded files are numeric matrix.mat
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
标签
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)