Using a 'for' loop within the app designer
이전 댓글 표시
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
Walter Roberson
2023년 11월 28일
I do not see any lm0 or Im0 in your code?
Im(length(Im)-length(Im1)+1:end)=Im1;
It is not clear to me why you are always overwriting the last entries in the vector?
Anders
2023년 11월 29일
Anders
2023년 11월 29일
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.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!