write output to a .txt or .mat file

9 views (last 30 days)
Lu
Lu on 15 May 2011
Hi everyone!
I have a couple of huge data files in .txt format and I´m creating 15-min intervals for these files so that each huge file gets reduced to a size of 34x42. The data files have the following structure:
datetime name price1 price2 price3 up to price40
19.10.09 09:00:00 basf 30 35 33 .....
19.10.09 09:15:00 basf 30 35 33 .....
19.10.09 09:30:00 basf 30 35 33 .....
19.10.09 09:45:00 basf 30 35 33 .....
...
19.10.09 17:30:00 basf 30 35 33 .....
My first question is how can I save such a file structure into a .txt or .mat file? because date and name are strings, and all prices are doubles. How can I put all this together? I thought of using a cell array but Im not sure because I´m really new to matlab and I´m learning it by doing. Also I tried:
save myvariables.txt datetime name price1 price2 price3
but it wont work because datetime and name are strings a nd the prices are doubles.
I also tried this:
save myvariables.txt price1 price2 price3
but the problem is that when i load it, the variables are loaded into my workspace as single variables and I can´t see thefile as the structure I mentioned above. How can i do it then?
So my second question is, , how could I save all these reduced files into a single .txt file or .mat file?? what kind of loop can I use to append all these reduced files into one single file?
I would really appreciate any help because I don´t know what else to do !
Thank you so much for your help :) Have a nice week!
Lourdes

Answers (3)

bym
bym on 15 May 2011
How about
fprintf()
dlmwrite()
  1 Comment
Lu
Lu on 17 May 2011
Dear proecsm,
Thank you for your answer! Im trying it but still no sucess yet. Ill keep trying.
Have a nice day :)

Sign in to comment.


Ivan van der Kroon
Ivan van der Kroon on 17 May 2011
If you save and reload your data with
y=load();
you have a structure file anyhow. If you want to save your variables in time1.mat
save('time1.mat','datetime','name','price*');
(the * after price will save all the variables that start with price in their name, the so-called wildcard). If you now type
time1=load('time1.mat')
time1 =
price1: 30
price2: 35
price3: 33
name: 'basf'
datetime: '19.10.09 09:00:00'
Is this what your are looking for? If you might wonder how to implement this in a for-loop such that the number in 'time#.mat' goes up, you can use eval, as in
eval(['save(''time' num2str(k) '.mat'',''datetime'',''name'',''price*'');'])
where k is the number you want to assign to the mat-file. The double '' are to hold them inside the string class which eval requires in this case.
Furthermore, I would suggest to concatenate the prices in a single variable, i.e. price=[price1 price2 price3... ]. Just suggesting...
  1 Comment
Lu
Lu on 17 May 2011
Dear Ivan,
Thank you for the reply! My script reduces huge data files into an output matrix of size 34x42. The problem is that I have around 205 and i can´t run the script for each file individually. So I created a for loop so it would do the calculations for all 205 files, and it would store the variables I need in a matrix called Output for each file. I just need to append all these output matrices one below the other one and so on, so i can just get 1 final matrix of size 6970x42.
I guess i will open a new thread and explain it better there.
Thank you so much for your help though :)

Sign in to comment.


Ivan van der Kroon
Ivan van der Kroon on 18 May 2011
m=34;
n=42;
N=205;
A=zeros(m*N,42);
for j=1:N
%load matrix B, don't know how you saved it, so just assuming you do it here and have variable B of size 34x42.
A((j-1)*n+(1:n),:)=B;
end

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!