Formatting issue using readmatrix/readcell/readtable from an excel sheet in to MATLAB App Designer
Show older comments
I am devleoping an app which reads data from Matlab in to an excel sheet. Once one excel sheet has got enough data points I will ask the app to beign writing in to a new one and then also ask it to read some of the values from the excel sheet back in to MATLAB. The latter task is the porblem, I am very confident that the first two tasks work effecitvely. I will paste the whole code below however the issue is with reading from the excel sheet. I have tried a variety of different commands including: readmatrix/readcell/readtable, with all of them I encounter the same porblem which is when I read the values they are returned as either NaN or #DIV/0!. I have tried formatting the values that I read from the sheet differnelty hence single, double, character and string however that still produces one of the two results above. Here is my full code below with annotations:
function LoadmatlabdataButtonPushed(app, event)
load('25_Cycle_data.mat','Axis_1_J5','Y_J5','Z_J5');
app.Axes = Axis_1_J5;
app.Y_= Y_J5;
app.Z_ = Z_J5;
yes = msgbox("Data was successfully saved","Success");
%Here I am jsut loading exmaple data whereas once this app is
%fully developed it will read all this data in 1 data point at
%a time.
end
function ChooseFileLocationButtonPushed(app, event)
app.savefile = uigetdir('\\fsvr\f\IDT Project Work');
newfolder = cd(app.savefile);
% changing the current folder location to the folder which my
% documents are loacetd in
end
function ProducedataButtonPushed(app, event)
%%
app.n = 1001; %inputted the amount of data points manually
for index = 1:app.n
app.Axes(index);
app.Y_(index);
pause(0.10);
app.Z_(index) ;
x_current = num2str(app.Axes(index));
app.showEditField.Value = x_current;
pause(0.25)
index
u = index/201;
%o = index/80;
%% This section is filename/file number formatting
if u == floor(u) && index > 200 %only once the data paoints has reached a value of 201 can they be written
% in to the excel sheet and consequnetly at multiples of 201 as well to
% cause the app to write it in to a new excel sheet.
if app.InputStartingFileNumberEditField.Value == 0
app.p = 0
elseif app.InputStartingFileNumberEditField.Value ~= 0
app.p = app.InputStartingFileNumberEditField.Value
app.p = (app.InputStartingFileNumberEditField.Value ) - 1
end
app.p = app.p + 1
app.og_filename = app.InputFileNameEditField.Value
source_input = char(app.og_filename)
source_s = string(source_input)
filenames = regexp(source_s,'\.','split')
dot = '.'
source = (filenames(1,1))
excel_s = filenames(1,2)
excel_c = strcat(dot,excel_s)
excel = char(excel_c)
underscore = "_"
q = string(app.p)
increment = strcat(underscore,q)
app.filename = strcat(source,increment,excel)
%% Writing the data to the excel sheet
sheet = 'Results';
xlRange_Xt = 'D2:D202';
writematrix(app.Axes,app.filename,'Sheet',sheet,'Range',xlRange_Xt);
xlRange_Y = 'G2:G202';
writematrix(app.Y_,app.filename,'Sheet',sheet,'Range',xlRange_Y);
xlRange_Z = 'H2:H202';
writematrix(app.Z_,app.filename,'Sheet',sheet,'Range',xlRange_Z);
%% Reading values from the excel sheet
sheet_2 = 'Conclusion';
xlRange_LAC = 'M8';
Repeatbility = readmatrix(app.filename,'Sheet',sheet_2,'Range',xlRange_LAC,'OutputType','char')
pause(5)
app.LAC_Y = Repeatbility(1,1)
app.LAC_Z = Repeatbility(6,1)
app.Backlash = Repeatbility(22,1)
yd = num2str(app.LAC_Y)
zd = num2str(app.LAC_Z)
backlashd = num2str(app.Backlash)
app.LACYRepeatabilityEditField.Value = yd
app.LACZRepeatabilityEditField.Value = zd
app.BacklashEditField.Value = backlashd
try
z < 0.44;
% once z has reached a vlue of less than 0.44 I want the program to stop
% running
break
catch
end
end
end
yes = msgbox("Data was successfully saved","Success");
end
end
I think the issue is that the MATLAB commands are reading the function in the formual box for that cell rather than the value that is caluated as a consequnece of the function. Hence, I have tried to read the values that I orginally wrote in to the spreadsheet and that worked fine because these values are just values in the formula box rather than a combination of excel functions and cell numbers.
Here is a screenshot of the page where I want to read the calculated values from:
The values in blue are the ones I want to read out from the excel sheet. However, I thought with that cell having had some formatting done to it I'd make the column M equal to those values as well to try and read that instead. Additionally, I tried using the VALUETOTEXT function for the column in N and then reading those values. Neither of those methods worked and it still returned either NaN or #DIV/0!.
3 Comments
Kevin Holly
on 11 Oct 2023
Have you tried using the Import Tool? Is it possible to share an example file?
dpb
on 11 Oct 2023
Simplify down to the basics w/o all the overhead of the app gui stuff first and get that to work...readtable, readmatrix and friends don't return formulas but the spreadsheet has to be set to recompute when new data are written. I've got several apps that update cell formulas and haven't seen such issues on returning results; are you sure you're converting the table values to numeric before writing them; that would explain reading back as nonnumeric if the data for the cells the formula references aren't numeric.
But, as @Kevin Holly says, we'd need a minimal functional example to be able to diagnose what's happening because w/o the spreadsheet content it's just not possible to know.
Joseph
on 11 Oct 2023
Accepted Answer
More Answers (0)
Categories
Find more on Spreadsheets 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!