Home > Archive > Matlab > December 2005 > extract column from matrix with loop
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
extract column from matrix with loop
|
|
| luca_cane@yahoo.com 2005-12-14, 10:02 pm |
| Hi all,
I am new at matlab and I have a problem.
I have multiple files all with 3 columns and variable number of rows.
I would like to extract the first and second column of each files and
made a two matrix: one with all the first column and one with all the
second column. At the moment I am using the following code, without any
success
files=dir('*.mat');
for k=1:length(files);
data = load(files(k).name);
matrix1(:,k) = data(:,1);
matrix2(:,k) = data(:,2);
end
If you could help me would be great.
Cheers,
Luca
| |
|
| luca_cane wrote:
> Hi all,
>
> I am new at matlab and I have a problem.
> I have multiple files all with 3 columns and variable number of
> rows.
> I would like to extract the first and second column of each files
> and
> made a two matrix: one with all the first column and one with all
> the
> second column. At the moment I am using the following code, without
> any
> success
>
> files=dir('*.mat');
> for k=1:length(files);
> data = load(files(k).name);
> matrix1(:,k) = data(:,1);
> matrix2(:,k) = data(:,2);
> end
<SNIP>
Luca, how did you create these .mat files? If they are flat files
with three columns of data then you need two changes: the load
command needs -ascii specified and you will need to take into account
the different number of rows, for example:
data = load('-ascii',files(k).name);
nrows = size(data,1);
matrix1(1:nrows,k) = data(:,1);
matrix2(1:nrows,k) = data(:,2);
You will then have 0s appended at the end of each column shorter than
the rest.
If the .mat files are actually Matlab .mat files then after the load
command, 'data' will be a structure whose field names correspond to
the name(s) of the variable(s) that were saved into the mat file and
you will need a different approach.
Arie.
| |
|
| luca_cane wrote:
>
>
> Hi all,
>
> I am new at matlab and I have a problem.
> I have multiple files all with 3 columns and variable number of
> rows.
> I would like to extract the first and second column of each files
> and
> made a two matrix: one with all the first column and one with all
> the
> second column. At the moment I am using the following code, without
> any
> success
>
> files=dir('*.mat');
> for k=1:length(files);
> data = load(files(k).name);
> matrix1(:,k) = data(:,1);
> matrix2(:,k) = data(:,2);
> end
>
>
> If you could help me would be great.
>
> Cheers,
>
> Luca
>
>
Besides the suggestions made by Arie, I would like to ask if your
code works when you run it for 1 file?
for k=1:1, ..
If your mat-files are really matlab files, I doubt that it does.
In what part of the code does the error occur?
What does your variable "data" contains after a succesfull load?
Does "who ('-file', files(k).name)" always give the same variable
name?
hth
Jos
| |
|
| Hi Luca,
check first if the data is loaded correctly with a breakpoint
then you can use
matrix1 = data(:,1);
matrix2 = data(:,2);
hth
kinor
ps to Arie, is your version faster?
| |
|
| kinor wrote:
<snip>
> ps to Arie, is your version faster?
>
I hadn't really considered that. I just noticed that the original
assignment (below) wouldn't work if some columns were of different
length.
[color=darkred]
Arie.
| |
| luca_cane@yahoo.com 2005-12-15, 7:07 pm |
| I generate the files through matlab.
when I open a single file in Matlab in the workspace I have the value
is the <mx3 single> where m is variable in each files.
when I try your code I end up with this error message
??? Error using ==> load
Unknown text on line number 1 of ASCII file C:\where\is \myfile.mat
"MATLAB".
I hope I gave enough info.
thank you
| |
| luca_cane@yahoo.com 2005-12-15, 7:07 pm |
| Hi Jos,
>Besides the suggestions made by Arie, I would like to ask if your
>code works when you run it for 1 file?
>for k=1:1, ..
I try the suggestions made by Arie but I still have prblem, when I run
it with only one files I got the following error:
??? Index exceeds matrix dimensions.
>Does "who ('-file', files(k).name)" always give the same variable
>name?
yes is always give me data as a name.
ty
Luca
| |
| luca_cane@yahoo.com 2005-12-15, 7:07 pm |
| Sorry about that. I thought it was on an other newsgroup.
it was not my twin brother by my alter ego ;)
cheers
Luca
| |
|
| luca_cane wrote:
<SNIP>
>Jos wrote:
>
> yes is always give me data as a name.
>
> ty
>
> Luca
OK, in this case the files are Matlab .mat files so you can use the
following code (Note: untested, I don't have access to Matlab at the
moment)
files = dir('*.mat');
for k=1:length(files);
temp = load(files(k).name);
nrows = size(temp.data,1);
matrix1(1:nrows,k) = temp.data(:,1);
matrix2(1:nrows,k) = temp.data(:,2);
end
Have a look at 'help save' and 'help load' to get more of an idea
about the way these .mat files work.
Good luck,
Arie.
| |
| luca_cane@yahoo.com 2005-12-17, 3:59 am |
| Hi Arie,
the code is working perfectly. I was just wondering if I could add the
zeros at the end of the column in matrix1 and 2.
Thanks a lot for your help I really appreciated.
Cheers,
Luca
|
|
|
|
|