Using a 'for' loop within the app designer

19 views (last 30 days)
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
%============================Organizing Data===============================
for k=1:Date_period
y = zeros(size(x));
[~,s] = unique(x,'first');
y(s) = x(s);
COVID_Matrix_Im0(k,:) = y;
Then I go on to plot the data. Thank you in advance for any thoughts, help,
Anders on 29 Nov 2023
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 on 30 Nov 2023
Edited: Walter Roberson on 30 Nov 2023
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.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 30 Nov 2023
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:

More Answers (0)


Find more on Loops and Conditional Statements 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!