I'm trying to use loop function and want to cut my code short, and my end goal is to compare idle components from 7 idle machines, from an excel data, I'm not sure how to do

1 回表示 (過去 30 日間)
  1. i want to add uigetfile function in my code so user can select the file wherever the location is, so whenever i'm trying it, it is asking me to open my excel files 2 times.
  2. i'm trying to compare idle components for example, i want to compare heater from machine 1 and heater from machine 6, same goes for any other 7 machines but for 7 machines.
  3. i've edited this question, because the answer i've got is for comparing the machines, thank you for that, but i want to compare components too from the machines,
  4. i've attached my excel file with it, so i can get a clear answer
i have linked my code below, where i managed to set the range , but i'm not sure how to call it twice for 2 components, and then will be able to compare them in same graph
% To use this code type in command window
% PLOT(name,Machine,range)
% To minimize the name of components, name is being used as
% Heater=heater
% Tube - Heater to Lower Solenoid= tubehls
% Solenoid (Lower)= solenoidl
% Tube - Lower Solenoid to Further Solenoids=tubelsfs
% Venturi Assembly (including lower L pipe)= venturi
% Solenoid Assembly (Middle and Upper)= solenoidmu
% Solenoid (Upper)= solenoidu
% Solenoid (Middle)= solenoidm
% Baffle with tubes connecting to Solenoid Assembly= baffle
% Steam Tip= steamtip
% Steam Wand and Tube= steamwt
% Tube - Pump to heater= tubeph
clc
clearvars
function PLOT(name,Machine)
[filename]=uigetfile('*.xlsx','Select File','MultiSelect','off');
drymass_index = 1;
final_index = 8;
massgain_index = 9;
massreduction_index = 10;
rateofscale_index= 11;
range = '';
x = {'heater','tubehls','solenoidl','tubelsfs','venturi','solenoidmu','solenoidu','solenoidm','baffle','steamtip','steamwt','tubeph'};
y = [5,6,7,8,10,12,13,14,16,17,18,19];
for i = 1:12
k = string(x(i));
if strcmp(name,k)
a = string(y(i));
r1 = strcat('C',a);
r2 = strcat(':J',a);
range = strcat(r1,r2);
end
end
t = name;
if strcmp(name,t)
Component = xlsread(filename,Machine,range);
Component(drymass_index);
Component_massgain = Component(1)-Component(8);
Component_massreduction = Component(1)-Component(8);
Component_rateofscale = Component(1)-Component(8);
sprintf('drymass=%0.5g | massgain=%0.5g | massreduction=%0.5g | rateofscale=%0.5g',Component(drymass_index),Component_massgain,Component_massreduction,Component_rateofscale)
bar(Component(1:7))
title('TESTING DATA')
xlabel('Pre/post descale')
ylabel('Mass gain/loss')
end
end
  2 件のコメント
Gargi Patil
Gargi Patil 2021 年 8 月 10 日
編集済み: Gargi Patil 2021 年 8 月 10 日
Hi,
My understanding of your query is that you would like to condense the multiple if statements in your code. You can use the following code to replace the multiple if statements:
if strcmp(name,'heater') || strcmp(name,'tubehls') || strcmp(name,'solenoidl') ... || strcmp(name,'tubeph')
machineName = xlsread(filename, Machine, range);
machineName(drymass_index);
machineName_massgain = machineName(1)- machineName(8);
machineName_massreduction = machineName(1)- machineName(8);
machineName_rateofscale = machineName(1)- machineName(8);
sprintf('drymass=%0.5g | massgain=%0.5g | massreduction=%0.5g | rateofscale=%0.5g', machineName(drymass_index), machineName_massgain, machineName_massreduction, machineName_rateofscale)
plot(machineName(1:6))
end
If this does not address the issue, please provide more details about your query.
per isakson
per isakson 2021 年 9 月 3 日
A minor detail. I find
name_list = {'heater','tubehls','solenoidl','tubelsfs','venturi','solenoidmu' ...
,'solenoidu','solenoidm','baffle','steamtip','steamwt','tubeph' };
if any( ismember( name, name_list ) )
% ...
end
more readable than
if strcmp(name,'heater') || strcmp(name,'tubehls') || etc
% ...
end

サインインしてコメントする。

採用された回答

Mathieu NOE
Mathieu NOE 2021 年 9 月 3 日
hello
as far as I understand your request , this is a modified code (see below)
i splitted a main section and a subfunction section with param as parameter structure in function argument
You can choose which machines you want to plot either one alone or multiples, here a demo for the 7 machines all together
NB : do not use existing matlab native functions for you own subfunctions (here you create PLOT which can hide the native matlab plot function; so I modified the name)
My bar (grouped) plot :
Code :
clc
clearvars
params.drymass_index = 1;
params.final_index = 8;
params.massgain_index = 9;
params.massreduction_index = 10;
params.rateofscale_index= 11;
filename = 'C:\Users\A0H36019\Documents\Test Data.xlsx';
Machine = [1:7];
[Component,leg_str] = MYPLOT(filename,'heater',Machine,params) ;
Component(params.drymass_index); % what is this line for ??
Component_massgain = Component(1)-Component(8); % these 3 lines (from here to below) do the same computation - really ?
Component_massreduction = Component(1)-Component(8); % again
Component_rateofscale = Component(1)-Component(8); % again
sprintf('drymass=%0.5g | massgain=%0.5g | massreduction=%0.5g | rateofscale=%0.5g',...
Component(params.drymass_index),Component_massgain,Component_massreduction,Component_rateofscale) ;
bar(Component') ; % NB see transpose ; now we have 8 groups of 7 bars groupped (7 machines)
ylim([165 180]);
title('TESTING DATA')
xlabel('Pre/post descale')
ylabel('Mass gain/loss')
legend(leg_str);
% To use this code type in command window
% PLOT(name,Machine,range)
% To minimize the name of components, name is being used as
% Heater=heater
% Tube - Heater to Lower Solenoid= tubehls
% Solenoid (Lower)= solenoidl
% Tube - Lower Solenoid to Further Solenoids=tubelsfs
% Venturi Assembly (including lower L pipe)= venturi
% Solenoid Assembly (Middle and Upper)= solenoidmu
% Solenoid (Upper)= solenoidu
% Solenoid (Middle)= solenoidm
% Baffle with tubes connecting to Solenoid Assembly= baffle
% Steam Tip= steamtip
% Steam Wand and Tube= steamwt
% Tube - Pump to heater= tubeph
function [Component,leg_str] = MYPLOT(filename,name,Machine,params)
% filename = 'C:\Users\A0H36019\Documents\Test Data.xlsx';
drymass_index = params.drymass_index;
final_index = params.final_index;
massgain_index = params.massgain_index;
massreduction_index = params.massreduction_index;
rateofscale_index= params.rateofscale_index;
range = '';
x = {'heater','tubehls','solenoidl','tubelsfs','venturi','solenoidmu','solenoidu','solenoidm','baffle','steamtip','steamwt','tubeph'};
y = [5,6,7,8,10,12,13,14,16,17,18,19];
for i = 1:12
k = string(x(i));
if strcmp(name,k)
a = string(y(i));
r1 = strcat('C',a);
r2 = strcat(':J',a);
range = strcat(r1,r2);
end
end
t = name;
if strcmp(name,t)
for ci = 1:length(Machine)
Component(ci,:) = xlsread(filename,Machine(ci),range);
% Component(drymass_index); % what is this line for ??
% Component_massgain = Component(1)-Component(8);
% Component_massreduction = Component(1)-Component(8);
% Component_rateofscale = Component(1)-Component(8);
% sprintf('drymass=%0.5g | massgain=%0.5g | massreduction=%0.5g | rateofscale=%0.5g',Component(drymass_index),Component_massgain,Component_massreduction,Component_rateofscale)
% bar(Component(1:7))
% title('TESTING DATA')
% xlabel('Pre/post descale')
% ylabel('Mass gain/loss')
% create some legend strings
leg_str{ci} = ['Machine :' num2str(Machine(ci))];
end
end
end
  2 件のコメント
Abhishek Gandhi
Abhishek Gandhi 2021 年 9 月 5 日
編集済み: Abhishek Gandhi 2021 年 9 月 5 日
i got the answer it was ylim[156 180] prohibiting me to get the other data, once i removed it, thank it worked THANK YOU

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by