How to use xlsread with a variable cell instead of a constant cell?

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)

...
[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

Thanks, I tried your option but I got the next error:
function varargout = programamylarsexcel(varargin) % PROGRAMAMYLARSEXCEL MATLAB code for programamylarsexcel.fig % PROGRAMAMYLARSEXCEL, by itself, creates a new PROGRAMAMYLARSEXCEL or raises the existing % singleton*. % % H = PROGRAMAMYLARSEXCEL returns the handle to a new PROGRAMAMYLARSEXCEL or the handle to % the existing singleton*. % % PROGRAMAMYLARSEXCEL('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in PROGRAMAMYLARSEXCEL.M with the given input arguments. % % PROGRAMAMYLARSEXCEL('Property','Value',...) creates a new PROGRAMAMYLARSEXCEL or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before programamylarsexcel_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to programamylarsexcel_OpeningFcn via varargin. % % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help programamylarsexcel
% Last Modified by GUIDE v2.5 19-Jun-2015 11:15:32
% Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @programamylarsexcel_OpeningFcn, ... 'gui_OutputFcn', @programamylarsexcel_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end
if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT
% --- Executes just before programamylarsexcel is made visible. function programamylarsexcel_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to programamylarsexcel (see VARARGIN)
% Choose default command line output for programamylarsexcel handles.output = hObject;
% Update handles structure guidata(hObject, handles);
% UIWAIT makes programamylarsexcel wait for user response (see UIRESUME) % uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line. function varargout = programamylarsexcel_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure varargout{1} = handles.output;
% --- 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'); h=xlsread('Prestamos.xlsx','Hoja1',e); if b==1 set(handles.Location,'string','Mylar does not exist') else if a==h set(handles.Location,'string','Mylar loaned') else set(handles.Location,'string',c) set(handles.Badge,'string',f) end
end
ERROR Error using xlsread (line 164) Range argument must be a string. See HELP XLSREAD.
Error in programamylarsexcel>Search_Callback (line 86) h=xlsread('Prestamos.xlsx','Hoja1',f);
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in programamylarsexcel (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)programamylarsexcel('Search_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
>>
"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...)

Sign in to comment.

Categories

Tags

Asked:

on 22 Jun 2015

Edited:

dpb
on 23 Jun 2015

Community Treasure Hunt

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

Start Hunting!