problem plotting from function call
2 ビュー (過去 30 日間)
古いコメントを表示
I can perform two seperate overlay plots with the included script, but would like to call it from a function to make it more portable before i extend it. However, the function call only works with the full complement of arguments (as is).
Why can't i use global to reduce 3 potentially unecessary arguments (D,Fn,of)?
When i try they do not get recognised! How might i cure this?
0 件のコメント
採用された回答
Star Strider
2023 年 10 月 18 日
In general, global variables are not considered good programming practice, principally because the functions they are passed to can have the ability to change them. This makes debugging difficult. Passing them as parameters avoids this, because if a function changes the value of a parameter, it does so only within its own workspace, not globally. The value pased as a parameter doesn’t change unless the function specifically changes it and returns the changed value to the workspace as an output.
I added the ‘readFixedWidthFile’ function to read the fixed-width files in order to put the NaN values in the correct positions.
Try this —
clear, clc, close all
Uz = unzip('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1514334/Script.zip')
n = 3;
name = 'ofih';
D{3,1} = [];
fn{3,1} = [];
fullname = strcat(name,'%d.txt');
isp = 'Specific impulse I_{sp} [s]';
ispof = 'Specific impulse versus mixture ratio';
cstar = 'Characteristic velocity c* [m/s]';
cstarof = 'Characteristic velocity versus mixture ratio';
of = 'Mixture ratio (o/f)_m';
for k = 1:n
% filename = sprintf(fullname, k);
% fn{k,:} = filename;
fn{k,:} = Uz{k};
A = readFixedWidthFile(fn{k});
% fidi = fopen(filename,'rt');
% C = textscan(fidi, '%f%f%f', 'HeaderLines',3, 'CollectOutput',1);
% A = cell2mat(C);
D{k,:} = A(1:size(A,1)-3,:);
end
% fclose(fidi);
%
% Out = cell2mat(D);
figure('name','isp')
hold on
for k = 1:numel(D)
plot(D{k}(:,1), D{k}(:,2))
title(ispof)
xlabel(of)
ylabel(isp)
end
hold off
grid
legend(fn,'Location','best')
figure('name','cstar')
hold on
for k = 1:numel(D)
plot(D{k}(:,1), D{k}(:,3))
title(cstarof)
xlabel(of)
ylabel(cstar)
end
hold off
grid
legend(fn,'Location','best')
plotting('isp',isp,ispof,2,D,fn,of);
plotting('cstar',cstar,cstarof,3,D,fn,of);
function [] = plotting(name1,name2,name3,n,D,fn,of)
%global D fn of
figure('name',name1)
hold on
for k = 1:numel(D)
plot(D{k}(:,1), D{k}(:,n))
title(name3)
xlabel(of)
ylabel(name2)
end
hold off
grid
legend(fn,'Location','best')
disp('D')
disp(D)
disp('fn')
disp(fn)
disp('of')
disp(of)
end
function DataMatrix = readFixedWidthFile(filename)
opts = fixedWidthImportOptions('NumVariables',3, 'VariableWidths',[15 16 16], 'DataLines',[4 25; 27 28]);
T1 = readtable(filename, opts);
% T1(end-1:end,:)
% % missing = varfun(@ismissing, T1);
% % missing(end-1:end,:)
T2 = varfun(@(x)fillmissing(x,'constant',{'NaN'}), T1);
% T2(end-1:end,:)
%
% format longG
% A = str2double(table2array(T2))
% A(end-1:end,:)
DataMatrix = str2double(table2array(T2));
end
% ========= END: Brantosaurus_2023_10_18.m ==========
.
2 件のコメント
Star Strider
2023 年 10 月 18 日
My pleasure!
Definitely without global. Always pass the extra arguments if you need to (if the function needs them), since that works.
The problem with not having access to the R2017a documentation is that I was not aware of that limitation. My apologies.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Data Distribution Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!