Create "n" variables from an excel file then create "n" equations for each variable

조회 수: 1 (최근 30일)
I'm fairly new to matlab and this seems like a simple problem to fix. I am doing a series of simple calculations from data in an excel file. Currently I am typing in each varbiable by hand and indexing it from the array. I am looking for a way to automate this so that for each component I add in the excel file Matlab will generate all of the varables and equations I need.
%% Clear Windows
clear;
clc;
%% Import data and Define variables
Data_table = xlsread('weight_COM_data');
W1=Data_table(1,2);
W2=Data_table(2,2);
W3=Data_table(3,2);
H1=Data_table(1,3);
H2=Data_table(2,3);
H3=Data_table(3,3);
L1=Data_table(1,4);
L2=Data_table(2,4);
L3=Data_table(3,4);
Q1=Data_table(1,5);
Q2=Data_table(2,5);
Q3=Data_table(3,5);
X1=Data_table(1,7);
Y1=Data_table(1,8);
Z1=Data_table(1,9);
X2=Data_table(2,7);
Y2=Data_table(2,8);
Z2=Data_table(2,9);
X3=Data_table(3,7);
Y3=Data_table(3,8);
Z3=Data_table(3,9);
WT=Data_table(1,6);
Specific_weight=0.283;
%% Calculate Total Volume and Total Weight
Volume1=(((W1*H1*L1)-((W1-(WT*2))*(H1-(WT*2))*L1))*Q1);
Volume2=(((W2*H2*L2)-((W2-(WT*2))*(H2-(WT*2))*L2))*Q2);
Volume3=(((W3*H3*L3)-((W3-(WT*2))*(H3-(WT*2))*L3))*Q3);
TotalVolume=Volume1+Volume2+Volume3;
TotalWeight=TotalVolume*Specific_weight;
%% Calculate Center of Mass
Weight1=Volume1*Specific_weight;
Weight2=Volume2*Specific_weight;
Weight3=Volume3*Specific_weight;
XBar=(X1*Weight1+X2*Weight2+X3*Weight3)/TotalWeight;
YBar=(Y1*Weight1+Y2*Weight2+Y3*Weight3)/TotalWeight;
ZBar=(Z1*Weight1+Z2*Weight2+Z3*Weight3)/TotalWeight;
As you can see as the number of components on the excel file increases I would have a create a new variable and equation for each
Note: I'll attach the excel file that contains my inital data
Thanks Gray
  댓글 수: 2
Allen
Allen 2020년 1월 29일
Is there any particular reason why you need to have the data assigned to multiple variables consisting of a single value? If not, then you should import data as a table or array and perform row-wise calculations across the table "variables" (here "variables" is how MATLAB refers to columns of a table-class) or array columns.
Additionally, unless you have need to perform additional calculations or generate plots, then it might be simplier to just use formulas in Excel to obtain the calculations you have provided.
Mohammad Sami
Mohammad Sami 2020년 1월 29일
편집: Mohammad Sami 2020년 1월 29일
Matlab allows you do calculation on matrices and vectors. You don't have to store each value in a separate variable.
I would recommend you to complete the Matlab onramp course.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by