kaja
kaja

Reputation: 55

Efficient way to implement rather than structures in Matlab

I am reading some data from a csv or text file ( which consists of thousands of rows with each row consists of fixed number of columns - e.g.: 20).

I am keeping the above details in matlab with a structure as follows.

initial_var(firs).second_var(sec).third_var(thir).time(end+1, :) = [];
initial_var(firs).second_var(sec).third_var(thir).scan(end+1, :) = [];

initial_var(firs).second_var(sec).third_var(thir).time(end+1, :) = val1;
initial_var(firs).second_var(sec).third_var(thir).scan(end+1, :) = val2;

where first, sec, thir, val1, val2 are filled from the csv/text file. There are multiple fields available other than time and scan but I have not included them here.

While running the program, I am getting the warning

The variable initial_var appears to change size on every loop iteration. Consider preallocating for speed.

I know this can be solved by preallocating and initializing.

But my question here is, what is the better way of keeping the above data rather than the above mentioned structure type?

Upvotes: 0

Views: 162

Answers (2)

Nzbuu
Nzbuu

Reputation: 5251

These lines won't do anything:

initial_var(firs).second_var(sec).third_var(thir).time(end+1, :) = [];
initial_var(firs).second_var(sec).third_var(thir).scan(end+1, :) = [];

It means "delete the row after the end of this array".

You might like to look at a multi-dimensional structure:

vars(firs,sec,thr).time(end+1, :) = val1
vars(firs,sec,thr).scan(end+1, :) = val2

Should be easier to initialise too.

Also, when loading the data, you might like to look at textscan.

Upvotes: 1

John Colby
John Colby

Reputation: 22588

Typically, the fastest and most flexible way to read data is with fscanf. (See also csvread for a convenience wrapper for csv files.) For example:

data = randn(1e4, 20);
save data.txt data -ASCII

tic
h = fopen('data.txt')
data_read = fscanf(h, '%f');
data_read = reshape(data_read, 1e4, []);
toc
Elapsed time is 0.089097 seconds.

If the data are all numeric, then it is fastest to store and operate on simple matrices.

Also, if you post some specific data and reproducible code, we might be able to give more specific answers...

Upvotes: 0

Related Questions