How to use xlsread with a variable cell instead of a constant cell?
Show older comments
I'm using xlsread but I don't know which is the cell to get the info that I want to show in matlab program. The cell number is saved in a variable [d,e]. This is the program:
% --- Executes on button press in Search.
function Search_Callback(hObject, eventdata, handles)
% hObject handle to Search (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
a=str2double(get(handles.Partnumber,'string'));
xlswrite('Excelbasededatos.xlsx',a,'Hoja1','F3');
[b,c]=xlsread('Excelbasededatos.xlsx','Hoja1','G3');
[d,e]=xlsread('Excelbasededatos.xlsx','Hoja1','H3')
%Excelbasededatos H3 is where is located the cell to get
% the info from the file ´´Prestamos´´, but it shows an error.
g=xlsread('Prestamos','Hoja1',[d,e])
if b==1
set(handles.Location,'string','Mylar does not exist')
else
if a==g
set(handles.Location,'string','Mylar loaned')
else
set(handles.Location,'string',[b,c])
set(handles.Badge,'string',[d,e])
end
end
I hope you can help me on this.
Thanks in advance.
Answers (1)
dpb
on 22 Jun 2015
...
[d,e]=xlsread('Excelbasededatos.xlsx','Hoja1','H3')
g=xlsread('Prestamos','Hoja1',[d,e])
In the xlsread return above, d will be a numeric value of the cell 'H3' if it is numeric content (or empty if not) and e will be the text content of the cell if not numeric.
Hence, since this is supposedly a location, it would be variable e that should be the location of interest and the subsequent read should be
g=xlsread('Prestamos','Hoja1',e);
Have you confirmed what the two variables are, indeed, though??? BIG HINT: Use the debugger...
2 Comments
Luis Acevedo
on 22 Jun 2015
"ERROR Error using xlsread (line 164) Range argument must be a string."
Well, then the content of H3 of the preceding call wasn't a valid cell reference string then, after all, was it?
Did you take the previous advice and use the debugger to check? Have you verified the content of the spreadsheet?
We really can't do anything much here since we can't run your code or even look at the content of the spreadsheet in question. (HINT: Attach the spreadsheet or at least show the results of the preceding call as suggested...)
Categories
Find more on File Operations 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!