I want to read an excel file by entering the column and first line and last line.

17 views (last 30 days)
Is there an ability in matlab by which I could read any file that I want. for example I have written a code as follows
q_c = readmatrix('CPTU1.xlsx','Sheet','results','Range','B3:B2993');
f_s = readmatrix('CPTU1.xlsx','Sheet','results','Range','C3:C2993');
U_2 = readmatrix('CPTU1.xlsx','Sheet','results','Range','D3:D2993');
nrows = 2991;
in this code I have to write name of the file sheet range and then calculate number of rows manually for each of my files. there are softwares in which for exaple ask the file and then column and first line and last line. can I write a code in this form. because I have a lot of file that I have to analyze them (their row number are different). If for each of them I want to modify I will have a lot of m-file and also it wastes my time.
  5 Comments
dpb
dpb on 7 Jun 2022
Edited: dpb on 7 Jun 2022
OK, it so happens I have an app that lets user select files and sheets to use from a menu; it's not much code at all to do -- here are the two menu callback routines for one file and then the sheet...
% Menu selected function: BillingFileMenu
function BillFileFunction(app, event)
filestr=fullfile(app.billPath,'*Bill*.xlsx');
[app.billFile,app.billPath]=uigetfile(filestr,'Select Desired Billing File',"MultiSelect","off");
app.billQualFile=fullfile(app.billPath,app.billFile);
if isempty(app.billSheet),app.billSheet='Must Set Billing Sheet';end
try
app.BillingFolderTextArea.Value=app.billPath;
app.BillingFfileTextArea.Value=app.billFile;
app.BillingSheetTextArea.Value=app.billSheet;
catch
% leave unchanged; if nothing there, catch it when trying to run update
end
end
% Menu selected function: BillingSheetMenu
function BillingSheetMenuSelected(app, event)
if ~exist(app.billQualFile,'file')
errordlg([app.billQualFile "Not Found. Must Set Billing File First."])
return
end
sheets=sheetnames(app.billQualFile);
if isempty(app.billSheet), app.billSheet=sheets(1); end
iv=find(contains(sheets,app.billSheet,'IgnoreCase',false));
if isempty(iv)||numel(iv)>1, iv=1; end
ix=listdlg('PromptString','Select Billing Sheet','ListString',sheets,'selectionmode','single','initialvalue',iv);
if isempty(ix), ix=iv; end
app.billSheet=sheets(ix);
app.BillingSheetTextArea.Value=app.billSheet;
end
A MATLAB application uses a global structure named app the fields in it are either user-definable or the provided GUI component handles; all the file and sheet functions need to do is to store the user-selected values into the appropriate global variable; when the "Start" button is pressed, it uses the file name and sheet as the arguments to pass to readtable -- well, that routine isn't much to look at, either...
% Button pushed function: UpdateButton
function UpdateButtonPushed(app, event)
app.UpdateButton.Enable='off'; app.UpdateButton.Text="Working"; app.UpdateButton.FontColor='r';
app.QuitButton.Enable='off';
drawnow nocallbacks
if ~isfile(app.billQualFile)
h=errordlg([app.billQualFile "Not Found. Must Set Billing File First."],'modal');
waitfor(h)
app.UpdateButton.Text="Update"; app.UpdateButton.FontColor='k'; app.UpdateButton.Enable='on';
app.QuitButton.Enable='on';
drawnow
return
end
sheets=sheetnames(app.billQualFile);
if ~contains(sheets,app.billSheet,'IgnoreCase',false)
h=errordlg([app.billSheet "Not Found. Must Set Billing Sheet First."]);
waitfor(h)
app.UpdateButton.Text="Update"; app.UpdateButton.FontColor='k'; app.UpdateButton.Enable='on';
app.QuitButton.Enable='on';
drawnow
return
end
% Make sure user doesn't have file locked/open in another process first...
% Excel creates hidden file with "~$" prefix while open; search for this file
chkOpen=true;
while chkOpen
[~,fn]=system(['dir /a:h /b "' fullfile(app.awardPath,['~$' app.awardFile]) '"']);
chkOpen=matches(strtrim(fn(3:end)),app.awardFile,'IgnoreCase',true);
if chkOpen
h=errordlg([app.awardFile "Locked for Edit. Must Close Award File First."], ...
'File Locked',"non-modal");
waitfor(h)
end
end
% If UpdateSplits requested make sure user doesn't have bill file open, too
% Excel creates hidden file with "~$" prefix while open; search for this file
if app.billUpdate
chkOpen=true;
while chkOpen
[~,fn]=system(['dir /a:h /b "' fullfile(app.billPath,['~$' app.billFile]) '"']);
chkOpen=matches(strtrim(fn(3:end)),app.billFile,'IgnoreCase',true);
if chkOpen
h=errordlg([app.billFile "Locked for Update. Must Close Billing File First."], ...
'File Locked',"non-modal");
waitfor(h)
end
end
end
if ~isfile(app.billQualFile)
h=errordlg([app.billQualFile "Not Found. Must Set Billing File First."],'modal');
waitfor(h)
app.UpdateButton.Text="Update"; app.UpdateButton.FontColor='k'; app.UpdateButton.Enable='on';
app.QuitButton.Enable='on';
drawnow
return
end
% insert a progress dialog for grins...
h=uiprogressdlg(app.RestrictedFundsAwardsWorkbookUpdateToolUIFigure, ...
"Title",'Please Standby...',"Message",'Reading Billing',"Indeterminate","on");
% and finally, do the work...
[tBill,tPay]=readBilling(app.billQualFile,app.billSheet,app.billUpdate);
h.Message='Writing Awards';
writeAwards(tBill,tPay,app.year,app.semester,app.awardQualFile,app.awardSheet);
h.Message='Update Complete';
pause(0.5)
close(h)
app.UpdateButton.Text="Update"; app.UpdateButton.FontColor='k'; app.UpdateButton.Enable='on';
app.QuitButton.Enable='on';
drawnow
% see if user asked to open file...
if app.OpenFileOnCompletionCheckBox.Value
winopen(app.awardQualFile)
end
end
Almost all of the above is just error checking and doing the housekeeping for the GUI; if one just looks at the sequence, it all boils down to just one line
% Button pushed function: UpdateButton
function UpdateButtonPushed(app, event)
% error handling code elided for brevity...
%...
% to here
% and finally, do the work...
[tBill,tPay]=readBilling(app.billQualFile,app.billSheet,app.billUpdate);
% resetting to activate buttons when done processing code elided for brevity...
%...
% to here
end
that passes the file name and sheet to the working function that in turn calls readtable with those to open the specific file and sheet.
As suggested, it doesn't bother to try to fix the range to read a priori; it reads the file and infers from it the size and knows what to do with the data in the file.

Sign in to comment.

Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!