How to extract data efficiently from table or 2D array?

I imported data from excel into MATLAB. I want to use MATLAB's curve fiiting toolbox to explore relationship between different variables. Is there a more efficient way to extract data rather than doing it for each column? Please note I want to access all columns separately. Excel spreadsheet attached.
T = importdata('SelfStorageData.xlsx');
T1 = T.data.ExampleLeases;
T2 = T.data.TypicalMoveIn;
T3 = T.data.TypicalMoveOut;
unit = T1(:,1);
type_indicator = T1(:,3) ;
width = T1(:,4);
length = T1(:,5);
days_available = T1(:,6);
days_occupied = T1(:,7);
time_occupied = T1(:,8);
average_stay = T1(:,9);
first_occupancy = T1(:,10);
average_lease = T1(:,11);
occ = T1(:,12);
standard_rate = T1(:,13);
lease_rate = T1(:,14);
effective_rate = T1(:,15);
last_standard_rate_change = T1(:,16);
last_lease_rate_change = T1(:,17);
Also, If I read table, and want to use curve fitting toolbox, how can I extract data from each column of the table in a more efficient manner?
T1 = readtable('SelfStorageData.xlsx', 'Sheet', 'Example Leases');
T2 = readtable('SelfStorageData.xlsx', 'Sheet', 'Typical Move In');
T3 = readtable('SelfStorageData.xlsx', 'Sheet', 'Typical Move out');
T1.Properties.VariableNames = {'unit' 'typeCharacter' 'typeIndicator'...
'width' 'length' 'daysAvailable' 'daysOccupied' 'timesOccupied' 'averageStay'...
'firstOccupancy' 'averageLease' 'Occ.' 'standardRate' 'leaseRate' 'effectiveRate'...
'lastStandardRateChange' 'lastLeaseRateChange'};
type_indictor = T1.typeIndicator;
width = T1.width;
length = T1.length;
days_available = T1.daysAvailable;
days_occupied = T1.daysOccupied;
time_occupied = T1.timesOccupied;

回答 (1 件)

Adam Danz
Adam Danz 2020 年 6 月 23 日

0 投票

Extracting data from a matrix/table into individual variables is the most inefficient way to use those values.
Data stored in a matrix/table is tidy, well organized, efficient, and compact. Use indexing instead. Indexing is one of the superpowers of Matlab. For example, instead of extracting column 9 to variable average_stay, just use data(:,9) as the input vector.
Here's some background on indexing with matrices
and with tables

4 件のコメント

Purnjay Peshawaria
Purnjay Peshawaria 2020 年 6 月 23 日
Thank you for your answer. I want to access individual variables so that I can assign X data and Y data as part of curve fitting. See image below.
Adam Danz
Adam Danz 2020 年 6 月 23 日
Look at the variables in the command window so you can see their size, shape, etc.
Judging from the code in your question, T1 is a matrix, days_occupied is a column vector. So that error message makes perfect sense.
Purnjay Peshawaria
Purnjay Peshawaria 2020 年 6 月 23 日
I think I need to make my point clear. Lets say I want to explore the relationship between days_available i.e T1(:,6) and days_occupied i.e. T1(:,7) . If I don't do what I'm doing (extracting data from matrix into individual variables), how can I populate X data and Y data in the above figure? You are suggesting I can simply access 9th column as data(:,9). But I need to name it because I can't put this directly in X data.
Adam Danz
Adam Danz 2020 年 6 月 23 日
In that case, it looks like you'll need to break apart the arrays/tables into separate variables and there's no shortcut to what you're already doing.
However, the curve fitting tool is good for explortation, to fine-tune your fitting proceedure. But in the end, you should use the fitting functions directly and for that, you can using indexing.

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

カテゴリ

質問済み:

2020 年 6 月 23 日

コメント済み:

2020 年 6 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by