Nested Functions Matlab App Designer

13 visualizaciones (últimos 30 días)
Santiago Chazaro
Santiago Chazaro el 29 de Jun. de 2023
Editada: Santiago Chazaro el 29 de Jun. de 2023
Hi,
I have the following functions in Matlab App Designer:
function [volatility] = funfour(~,file_reqs, file_reqs_size, chosen_date, name, object_type)
% Function: funfour
% This function outputs the Volatility of the file as a percentage when
% compared to a chosen date
% Inputs:
% file_reqs: table/cell
% file_reqs_size: double/cell
% chosen_date: datetime
%
% Outputs:
% volatility: double/array
%
% Traceability
%
% Author: Santiago Chazaro
%
% email:
%------------- BEGIN CODE -------------
if isa(file_reqs, 'table') == 1 % Determines the amount of files being checked
vol = zeros(file_reqs_size,1); %Pre Allocation
for i = 1:file_reqs_size
vol(i) = file_reqs.LastModifiedOn(i,1) >= chosen_date; % Determines if a date is before or after the chosen date and creates a logical array where 1 = after, 0 = before
end
volatility = sum(vol)/file_reqs_size*100; % Finds the percentage of requirements that were modified after the chosen date
app.volgraphfun(volatility, name, object_type, chosen_date); %<<<<<<<<<<
else
vol{1,size(file_reqs_size,1)} = []; %Pre Allocation
volatility = zeros(1,size(file_reqs_size,1)); % Pre Allocation
for k = 1:size(file_reqs_size,1)
for y = 1:file_reqs_size(k,1)
vol{1,k}(y,1) = file_reqs{1,k}.LastModifiedOn(y,1) > chosen_date; % Determines if a date is before or after the chosen date and creates a logical array where 1 = after, 0 = before
end
volatility(1,k) = sum(vol{1,k})/file_reqs_size(k,1)*100; % Finds the percentage of requirements that were modified after the chosen date
end
app.volgraphfun(volatility, name, object_type, chosen_date); %<<<<<<<<<<
end
end
Which calls the following function inside of it:
function [] = volgraphfun(~,volatility, name, object_type, cdr)
% Function: volgraphfun
% This function plots the volatility of a file
%
% Inputs:
% volatility: double/array
% name: char/cell
% object_type: char
% cdr: datetime
%
% Outputs:
% stacked bar graph
%
% Traceability
%
% Author: Santiago Chazaro
%
% email:
%------------- BEGIN CODE -------------
if isa(name, 'char') % For single file inputs
op_vol = 100 - volatility; % Calculates nonvolatilty
yaxis = [volatility; op_vol]; % Concacates values
namecell = cellstr(name); % converts name to cell array
xaxis = categorical(namecell); % Creates categorical axis
figure('Name', name) % Defines figure
bar(xaxis, yaxis, 'stacked'); % Plots volatility as a stacked bar chart
barbase = cumsum([zeros(size(yaxis,1),1) yaxis(:, 1:end-1)],2); % Finds the cumulative sum of the y axis to determine positioning for legend
barbase = yaxis./2 + barbase; % Halfs the calculated values
for i = 1:size(yaxis,1)
text(xaxis, barbase(i), sprintf('%.01f %%', yaxis(i)), 'HorizontalAlignment', 'center'); % Places names in middle of bars
end
title(sprintf('Volatility of %s', name));
subtitle(sprintf('CDR: %s', cdr));
ylabel('Percentage');
legend(sprintf('%s objects changed after CDR', object_type), sprintf('Stable %s objects after CDR', object_type))
else % Same code as above, for multiple inputs
for ii = 1:size(name, 2)
op_vol(ii) = 100 - volatility(ii);
yaxis{1,ii} = [volatility(ii); op_vol(ii)];
namecell = cellstr(name{1,ii});
xaxis = categorical(namecell);
figure('Name', name{1,ii})
bar(xaxis, yaxis{1,ii}, 'stacked');
barbase{1,ii} = cumsum([zeros(size(yaxis{1,ii},1),1) yaxis{1,ii}(:, 1:end-1)],2);
barbase{1,ii} = yaxis{1,ii}./2 + barbase{1,ii};
for i = 1:size(yaxis{1,ii},1)
text(xaxis, barbase{1,ii}(i), sprintf('%.01f %%', yaxis{1,ii}(i)), 'HorizontalAlignment', 'center')
end
title(sprintf('Volatility of %s', name{1,ii}));
subtitle(sprintf('CDR: %s', cdr));
ylabel('Percentage');
legend(sprintf('%s objects changed after CDR', object_type), sprintf('Stable %s objects after CDR', object_type));
end
end
end
However, I keep getting the following error:
Unable to resolve the name 'app.volgraphfun'.
Error in Traceability_imbedded_functions/funfour (line 483)
app.volgraphfun(volatility, name, object_type, chosen_date);
Error in Traceability_imbedded_functions/RunButtonPushed (line 1163)
[app.volatility_B] = app.funfour(app.file_B_reqs, app.file_B_reqs_size, app.chosen_date_1, app.file_B_name, app.object_type);
Error in matlab.apps.AppBase>@(source,event)executeCallback(appdesigner.internal.service.AppManagementService.instance(),app,callback,requiresEventData,event) (line 63)
newCallback = @(source, event)executeCallback(appdesigner.internal.service.AppManagementService.instance(), ...
483 app.volgraphfun(volatility, name, object_type, chosen_date);
Is this a problem with the function referencing ?
Note: The above functions are all inside the app itself, and I'd like to keep it that way if possible.

Respuesta aceptada

VBBV
VBBV el 29 de Jun. de 2023
app.volgraphfun(volatility, name, object_type, chosen_date);

Change the above lines to

app.volgraphfun(~, volatility, name, object_type, chosen_date);

Since function seems to be defined for 5 arguments

  3 comentarios
VBBV
VBBV el 29 de Jun. de 2023

Ok. Then volgraphfun seems to be subfunction of funfour instead of component function. So, instead of

app.volgraphfun(volatility, name, object_type, chosen_date);

Call it just using its name as below

volgraphfun(volatility, name, object_type, chosen_date);
Santiago Chazaro
Santiago Chazaro el 29 de Jun. de 2023
Editada: Santiago Chazaro el 29 de Jun. de 2023
Ok, I think I understand the logic, instead of defining volgraph in the main app, I should just define it inside of funfour correct?
Edit: Yes
function [volatility] = funfour(~,file_reqs, file_reqs_size, chosen_date, name, object_type)
% Function: funfour
% This function outputs the Volatility of the file as a percentage when
% compared to a chosen date
% Inputs:
% file_reqs: table/cell
% file_reqs_size: double/cell
% chosen_date: datetime
%
% Outputs:
% volatility: double/array
%
% Traceability
%
% Author: Santiago Chazaro
%
% email: santiago.chazaro@ngc.com
%------------- BEGIN CODE -------------
if isa(file_reqs, 'table') == 1 % Determines the amount of files being checked
vol = zeros(file_reqs_size,1); %Pre Allocation
for i = 1:file_reqs_size
vol(i) = file_reqs.LastModifiedOn(i,1) >= chosen_date; % Determines if a date is before or after the chosen date and creates a logical array where 1 = after, 0 = before
end
volatility = sum(vol)/file_reqs_size*100; % Finds the percentage of requirements that were modified after the chosen date
volgraphfun(volatility, name, object_type, chosen_date);
else
vol{1,size(file_reqs_size,1)} = []; %Pre Allocation
volatility = zeros(1,size(file_reqs_size,1)); % Pre Allocation
for k = 1:size(file_reqs_size,1)
for y = 1:file_reqs_size(k,1)
vol{1,k}(y,1) = file_reqs{1,k}.LastModifiedOn(y,1) > chosen_date; % Determines if a date is before or after the chosen date and creates a logical array where 1 = after, 0 = before
end
volatility(1,k) = sum(vol{1,k})/file_reqs_size(k,1)*100; % Finds the percentage of requirements that were modified after the chosen date
end
volgraphfun(volatility, name, object_type, chosen_date);
end
function [] = volgraphfun(volatility, name, object_type, cdr)
% Function: volgraphfun
% This function plots the volatility of a file
%
% Inputs:
% volatility: double/array
% name: char/cell
% object_type: char
% cdr: datetime
%
% Outputs:
% stacked bar graph
%
% Traceability
%
% Author: Santiago Chazaro
%
% email: santiago.chazaro@ngc.com
%------------- BEGIN CODE -------------
if isa(name, 'char') % For single file inputs
op_vol = 100 - volatility; % Calculates nonvolatilty
yaxis = [volatility; op_vol]; % Concacates values
namecell = cellstr(name); % converts name to cell array
xaxis = categorical(namecell); % Creates categorical axis
figure('Name', name) % Defines figure
bar(xaxis, yaxis, 'stacked'); % Plots volatility as a stacked bar chart
barbase = cumsum([zeros(size(yaxis,1),1) yaxis(:, 1:end-1)],2); % Finds the cumulative sum of the y axis to determine positioning for legend
barbase = yaxis./2 + barbase; % Halfs the calculated values
for i = 1:size(yaxis,1)
text(xaxis, barbase(i), sprintf('%.01f %%', yaxis(i)), 'HorizontalAlignment', 'center'); % Places names in middle of bars
end
title(sprintf('Volatility of %s', name));
subtitle(sprintf('CDR: %s', cdr));
ylabel('Percentage');
legend(sprintf('%s objects changed after CDR', object_type), sprintf('Stable %s objects after CDR', object_type))
else % Same code as above, for multiple inputs
for ii = 1:size(name, 2)
op_vol(ii) = 100 - volatility(ii);
yaxis{1,ii} = [volatility(ii); op_vol(ii)];
namecell = cellstr(name{1,ii});
xaxis = categorical(namecell);
figure('Name', name{1,ii})
bar(xaxis, yaxis{1,ii}, 'stacked');
barbase{1,ii} = cumsum([zeros(size(yaxis{1,ii},1),1) yaxis{1,ii}(:, 1:end-1)],2);
barbase{1,ii} = yaxis{1,ii}./2 + barbase{1,ii};
for i = 1:size(yaxis{1,ii},1)
text(xaxis, barbase{1,ii}(i), sprintf('%.01f %%', yaxis{1,ii}(i)), 'HorizontalAlignment', 'center')
end
title(sprintf('Volatility of %s', name{1,ii}));
subtitle(sprintf('CDR: %s', cdr));
ylabel('Percentage');
legend(sprintf('%s objects changed after CDR', object_type), sprintf('Stable %s objects after CDR', object_type));
end
end
end
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Standard File Formats en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by