MATLAB Answers

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

조회 수: 2(최근 30일)
Abhishek Gandhi
Abhishek Gandhi 2021년 8월 4일
댓글: Mathieu NOE 2021년 9월 6일
  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
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

추가 답변(0개)

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by