Using a 'for' loop within the app designer

9 次查看(过去 30 天)
Edited
I have code to model COVID data in 2-and-3D. The following code runs and is usable on its own, but I am trying to design a Matlab app that runs on the same things. Since I am new to the app designer feature of Matlab, I am having trouble inserting the code to run in my app. I want to plot COVID_Matrix_Im0, but it seems like the app only keeps the original variable COVID_Matrix_Im0 rather than updating with my 'for' loops.
The code is as follows:
clc; clear;
COVID19_Data = readtable('COVID-19-geographic-disbtribution-worldwide-2020-12-14.xlsx', 'ReadVariableNames', true,'PreserveVariableNames',true); % read data from the excel sheet
COVID19_Data(:,end) = [ ]; % Remove the last cumulative column since we can compute it accurately from Im
COVID19_Data.popData2019(isnan(COVID19_Data.popData2019))=0; % To prevent any zeros in the population field as it has been noticed
SearchCountry=COVID19_Data.countriesAndTerritories; % Choose the country name
CountryFullName=unique(COVID19_Data.countriesAndTerritories);% Country full name from the excel sheet
DateString=datestr(COVID19_Data.dateRep); % Date column in the string format
formatIn = 'dd-mmm-yyyy'; % Date format
Date_first = min(datenum(DateString,formatIn)); % First date of confirmed cases (Dec 31, 2019)
Date_end = max(datenum(DateString,formatIn)); % last date (up to today) of the confirmed cases
Date_period = Date_end - Date_first + 1; % Infection duration in days
t1 = datestr(Date_first); % Changing format of the first date
t2 = datestr(Date_end); % Changing format of the last up to date date
td = (Date_first:Date_end)'; % Time duration (numeric format)
tcovid=datestr(td); % Time duration (string format)
CountryCount=unique(SearchCountry); % Number of ountries without repeating
COVID_Matrix_Im=zeros(Date_period,length(CountryCount)); % Create a matrix for the infected cases
COVID_Matrix_Im0=COVID_Matrix_Im; % Create a final matrix for the infected cases
COVID_Matrix_Cm=zeros(Date_period,length(CountryCount)); % Create a matrix for the cumulative infected cases
COVID_Matrix_Cm0=COVID_Matrix_Cm; % Create a final matrix for the cumulative infected cases
COVID_Matrix_Dm=zeros(Date_period,length(CountryCount)); % Create a matrix for the death cases
COVID_Matrix_CDm=zeros(Date_period,length(CountryCount)); % Create a matrix for the cumulative death cases
COVID_Matrix_CDm0=COVID_Matrix_Cm; % Create a final matrix for the cumulative death cases
COVID_Matrix_Date=zeros(Date_period,length(CountryCount)); % Create a matrix for date
Im=zeros(Date_period,1); % Occupy locations of Im in the loop
Cm=zeros(Date_period,1); % Occupy locations of Cm in the loop
Dm=zeros(Date_period,1); % Occupy locations of Dm in the loop
COVID_Date=string(zeros(length(CountryCount), Date_period)); % Occupy locations of the date in the loop
%=========================Creating the Main Matrices=======================
for i=1:length(CountryCount)
RowIdx = find(strcmpi(CountryCount(i), SearchCountry)); % Row indices containing the input name
SelectedRows = COVID19_Data(RowIdx',:); % Portion of the data extracted for a given country
SelectedRowsFlipped = flipud(SelectedRows); % Flip the rows (the original data is organized from new (top) to old (bottom))
SelectedRowsFlipped.CumulativeI = cumsum(SelectedRowsFlipped.cases); % Add a column to the table to cumulatively sum I
Im1 = (abs(SelectedRowsFlipped.cases)); % Original measured infectious (from starting date to the end as in the Excel sheet)
Dm1 = (abs(SelectedRowsFlipped.deaths)); % Original measured deads (from starting date to the end as in the Excel sheet)
Cm1 = (SelectedRowsFlipped.CumulativeI); % Original cumulative sum of Im
Im(length(Im)-length(Im1)+1:end)=Im1;
Cm(length(Cm)-length(Cm1)+1:end)=Cm1;
Dm(length(Dm)-length(Dm1)+1:end)=Dm1;
COVID_Matrix_Im(:,i)=Im;
COVID_Matrix_Cm(:,i)=Cm;
COVID_Matrix_Dm(:,i)=Dm;
COVID_Matrix_CDm(:,i)=cumsum(Dm);
COVID_Date(i,:)=string(tcovid);
end
%============================Organizing Data===============================
for k=1:Date_period
x=COVID_Matrix_Im(k,:);
y = zeros(size(x));
[~,s] = unique(x,'first');
y(s) = x(s);
COVID_Matrix_Im0(k,:) = y;
end
Then I go on to plot the data. Thank you in advance for any thoughts, help,
  4 个评论
Anders
Anders 2023-11-29
To clarify, my exact problem, when I use surf(COVID_Matrix_Im0) in the app, I am only able to graph the inital matrix I set up with this code (all zeros):
COVID_Matrix_Im=zeros(Date_period,length(CountryCount)); % Create a matrix for the infected cases
COVID_Matrix_Im0=COVID_Matrix_Im; % Create a final matrix for the infected cases
rather than what I updated the code to be through my 'for' loops. Possibly might be an error in how I am implementing the for loops or how I am calling back to the variables within in App Designer, but I must be missing it
Taylor
Taylor 2023-11-30
编辑:Walter Roberson 2023-11-30
It's difficult to say without having the code for your entire app, but I suspect the issue may come down to Public/Private Properties within an App.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2023-11-30
I don't know what callback that code is for. Presumably you have a pushbutton on your GUI that says something like "Go!" or "Compute" or "Analyze" or something like that. And when you click that button, it runs your code.
One problem I can see is that you call clear as the second command in your code. Doing that will probably clear the very important "app" variable, which will cause all kinds of problems. There is no need to ever call clear inside a function so take that line out and see if it still works.
If you have any more questions, then attach your data and code (your .mlapp file) with the paperclip icon after you read this:

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by