error importing multiple files at same time - matlab

Trying to join different files with an specific suffix in a matrix, but always I obtain a matrix with unique row containing the values of the last file..
As example
I have multiple files like:
2302_Cabeza_L_x.txt, 2202_Cabeza_L_x.txt, 1702_Cabeza_L_y.txt.....
The code I'm using...
codes= [2302,2202,1602,1502,1702];
for p=1:length(codes)
name=mat2str(codes(:,p));
orden2=(name(2:length(name)-11));
orden=str2num(orden2);
allCABLX = importdata([name '_Cabeza_L_x.txt']);
allCABLY = importdata([name '_Cabeza_L_y.txt']);
allCABCY = importdata([name '_Cabeza_C_y.txt']);
allCABCX = importdata([name '_Cabeza_C_x.txt']);
end
Thank you!

You are overwriting the variables allCABLX, allCABLY, allCABCY and allCABCX in every iteration, so only the last values stay there after the loop. You need to save the data inside the loop to be able to access it afterwards.
If all files have the same number of entries, this can be achieved by concatenating the values obtained by importdata. Since I don't know the dimensions of the output of importdata, I'm not going into the details here.
If the files have different number of entries, you can use a cell array to store the data of each iteration. This works as well in case all entries are of the same size. The following code does exactly this for one of the variables:
codes= [2302,2202,1602,1502,1702];
allCABLX = cell(length(codes),1); % create empty cell array
for p=1:length(codes)
name=num2str(codes(:,p));
allCABLX{p} = importdata([name '_Cabeza_L_x.txt']);
end
Note that I replaced mat2str by num2str since you only have a number to convert and not a whole matrix. In case all the files have data of the same dimension, you can use cell2mat after the loop to get a normal matrix.

Related

Reading strings into individual array/matrix elements in Matlab

I have a text file that contains a number of strings, one per line, as below.
Happy
Sad
Disgust
Happy
Sad
Etc...
I want to be able to read these strings and store them into an array or matrix within Matlab. At the moment the code I have works, but it stores all of the strings into a single array element, like this.
HappySadDisgustHappySad...
I want the strings to be stored in their own individual elements.
What changes do I make to this code to make this happen?
emotionFile = fopen('emotion_labels_purged.txt','r');
formatSpec = '%s';
sizeEmotionLabels = [1 Inf];
emotionLabels = fscanf(emotionFile,formatSpec,sizeEmotionLabels)
fclose(emotionFile);

How to do preallocation in storing field variables in matlab

I am storing the field variables calculated in a for loop in a vector by appending the values, However I would like to preallocate first for performance. I tried to vectorize this operation but it does not give me what I would like to accomplish. I have put the example of the operation below. How do I do the preallocation in this? For speed.
j=('load raw.mat');
var=fields(j);
val_mat=[];
kk=fieldnames(j);
for i=(length(kk)-Var_no)+1:Var_no+(length(kk)-Var_no)
val_mat=[val_mat j.(var{i})];
end
Based on your code it looks like you are trying to grab all variables stored in raw.mat and concatenate them. To do this, you can replace the loop with struct2cell to convert all field values to a cell array of values and then use cat to concatenate them
data = load('raw.mat');
values = struct2cell(data);
val_mat = cat(2, values{:});
Since we have removed the loop, there is no need to pre-allocate.
I've also taken the liberty to rewrite your code as valid MATLAB code.

Create a loop using part of variable name in MATLAB

I am a beginner in Matlab and have not been able to find an answer to my question so far. Your help will definitely be very much appreciated.
I have 70 matrices (100x100), named SUBJ_1, SUBJ_2 etc. I would like to create a loop so that I would calculate some metrics (i.e. max and min values) for each matrix, and save the output in a 70x2 result matrix (where each row would correspond to the consecutively named SUBJ_ matrix).
I am struggling with both stages - how to use the names of individual variables in a 'for' loop and how to properly save individual outputs in a combined array.
Many thanks and all the best!
Don't use such variable names, create a big cell array named SUBJ and put each Matrix in it.
r=zeros(numel(SUBJ),2)
for idx=1:numel(SUBJ)
r(idx,1)=min(min(SUBJ{idx}))
r(idx,2)=max(max(SUBJ{idx}))
end
min and max are called twice because first call creates maximum among rows, second call among columns.
Even though this is in principle possible in Matlab, I would not recommend it: too slow and cumbersome to implement.
You could instead use a 3-D matrix (100x100x70) SUBJ which would contain all the SUBJ_1 etc. in one matrix. This would allow you to calculate min/max etc. with just one line of code. Matlab will take care of the loops internally:
OUTPUT(:,1) = min(min(SUBJ,[],1)[],2);
OUTPUT(:,2) = max(max(SUBJ,[],1)[],2);
Like this, OUTPUT(1,1) contains min(min(SUBJ(:,:,1))) and so on...
As to how to use the names of individual variables in a 'for' loop, here gives an example:
SUBJ = [];
for idx = 1:70
term = eval(['SUBJ_',num2str(idx)]);
SUBJ = [SUBJ; max(max(term)),min(min(term))];
end

Extracting variables while reading in data files

I am quite new to data analysis, so if this is a rookie question, I'm sorry, I am learning as I go.
I have just started doing some work in variable star astronomy. I have about 100 files for every night of observation that all contain the same basic information (star coordinates, magnitude, etc.). I am loading all of the files into my workspace as arrays using a for-loop
files = dir('*.out');
for i=1:length(files)
eval(['load ' files(i).name ' -ascii']);
end
I'm only really interested in two columns in each file. Is there a way to extract a column and set it to a vector while this for-loop is running? I'm sure that it's possible, but the actual syntax for it is escaping me.
try using load as a function and save it's output to a variable
files = dir('*.out');
twoCols = {};
for ii=1:length(files)
data = load( files(ii).name, '-ascii' ); % load file into "data"
twoCols{ii} = data(:,1:2); % take only two columns
end
Now variable twoCols holds the two columns of each file in a different cell.
You have to assign the load result to a new variable. Then if lets say your variable is starsInfo you can use
onlyTwoFirst = starsInfo(:,1:2)
That means take all the rows, but only columns 1 and 2.

copy looping matrixes into one 3-d matrix

I have a list of text files that I would like to load, and then extract rows where they all overlap. The first column contains years and each data set spans a different chunk of years but they all overlap in the middle. In the end I would like to have a three dimensional matrix with the overlapping years in one matrix. My code keeps getting stuck at the line that I have commented out. I know its incorrect but could anyone tell me why it is incorrect?
clear all
name_list = {'Beijing';'GT';'soi';'naoi';'Sydney_Airport';'Los Angeles';'Paris';'Presque Isle'};
[m,n] = size(name_list);
files = dir('*.txt');
[m,n] = size(files);
for i=1:m
eval(['load ' files(i).name ' -ascii']);
vals{i} = load(files(i).name);
matrix = vals{i};
station = (files(i).name(1:end-4));
startyear(i) = min(matrix(:,1));
endyear(i) = max(matrix(:,1));
allstart = max(startyear);
allend = min(endyear);
%matrixnew(i) = matrix(allstart:allend,2:13,i);
end
Two problems here:
Your commented line %matrixnew(i) = matrix(allstart:allend,2:13,i); assumes that matrix is a 3-d array, but elsewhere you treat it as 2-d (and I believe that load always returns a 2-d array). This could be why you are getting the "Index exceeds matrix dimensions" error. Example:
>> foo = rand(10,10);
>> foo(2:10,3:4,2)
Index exceeds matrix dimensions.
Maybe you want matrix(allstart:allend,2:13)? But that won't work, because allstart contains a year, which presumably will not be a valid index for the array (a more likely cause of your error). Using the index that contains the smallest value would be closer to being correct, but I think it still won't work.
matrixnew refers to a single element of an array. You can't assign an array to an element of an array. grantnz is right that making matrixnew a cell array would fix this error, and I guess that in the end you could turn your cell array into 3-d array.
I think you are on the right track, but are missing a few pieces to making this work. One thing to consider is that it looks like you are trying to do everything in a single pass. I don't see how that can work. You need to real all files before you can decide which range of years to keep. So do it in multiple passes: first load all data from all files into a cell array, then figure out the range of years, then pull the data from each file for that range of years.