Hi I want to generate some random matrices of size Nx3 for varying N, in GNU-Octave. I want to save each of these matrices to a different file.
The script below almost does the job, but strangely it produces only file with the name int2str(N) ; it keeps overwriting the files produced at the previous iteration.
for i=1:10
N=(2**i)
A=rand(N,3);
save int2str(N) A
end
The script is interpreting int2str(N) itself as a string. How do I avoid this behaviour?
You can invoke save with the function form:
save(int2str(N), "A")
Related
I am running a big set of simulations in Dymola using a script, so far, it works well.
However, it remains incomplete because all the results are still in .mat and I have not find a way to automatically save them as .csv.
I found the DataFiles.convertMATtoCSV() function, but it requires me to specify a list of variables to export. I would like it to export all the variables without writing them one by one, is it possible?
In the Dymola Manual, there is a section "Saving all values into a CSV file".
It contains the following example code:
// Define name of trajectory file (fileName) and CVS file
// (CSVfile)
fileName="PID_Controller.mat";
CSVfile="AllVariables.csv";
// Read the size of the trajectories in the result file and
// store in 'n'
n=readTrajectorySize(fileName);
// Read the names of the trajectories
names = readTrajectoryNames(fileName);
// Read the trajectories 'names' (and store in 'traj')
traj=readTrajectory(fileName,names,n);
// transpose traj
traj_transposed=transpose(traj);
// write the .csv file using the package 'DataFiles'
DataFiles.writeCSVmatrix(CSVfile, names, traj_transposed);
This should do what you want. Also it gives room for customization if necessary later...
I am running some simulations using R, which at the very end produce a matrix with the outcome. Since I run it under different conditions, I use append in the write.table command which writes it into a CSV file.
For a while, everything seemed to work fine. But then yesterday, something in the output CSV file seemed wrong: the order of the simulations was upside down, and one column looked unrealistic. Additionally, the column title written underneath the troublesome column got a "3" instead of the column name.
Since this was the result of the last simulation, and hence was still saved in the last matrix created, I could check it. The first columns of the output were fine, but then there was lack of correspondence between the last two columns in the file and the real output.
Writing the matrix into a file is the very last command on my program. Here is the command I use:
write.table(gen.dist, "FST2.csv", col.names=TRUE, row.names=F,sep=",", append=T)
gen.dist is a numeric matrix.
Has anyone encountered a similar problem?
Thank you
I have downloaded the "NYU Depth V2" dataset and toolbox from here. In the toolbox there is a script called get_synched_frames.m. I do not have Matlab, so I have tried running it in Octave. Unfortunately, it does not work as expected.
The line
% Faster than matlab's Dir function for big directories and slow
% distributed file systems...
files = regexp(ls(sceneDir), '(\s+|\n)', 'split');
gives only
files =
{
[1,1] = a-1300302776.479149-3987628315.dump
}
but ls(sceneDir) shows all files in the directory. Has anyone experienced this?
The difference is probably not in regexp, but in the return value of ls. ls does not behave the same way in Matlab and Octave when you capture its return value. Matlab's ls returns a char row vector (single string as char) with multiple files listed in it as a multi-line string with embedded newlines; Octave's ls returns a 2-D char array with one file per line. (IMHO Octave's format is better; it is very difficult to parse Matlab's ls output in a reliably correct manner. (That regexp code is not adequate.))
You might just want this in Octave:
files = cellstr(ls(sceneDir));
I have a code to calculate the mean of the first five values of each column of a file, for then use these values as a reference point for all set. The problem is that now I need to do the same but for many files. So I will need to obtain the mean of each file to then use these values again with the originals files. I have tried in this way but I obtain an error. Thanks.
%%% - Loading the file of each experiment
myfiles = dir('*.lvm'); % To load every file of .lvm
for i = 1:length(myfiles) % Loop with the number of files
files=myfiles(i).name;
mydata(i).files = files;
mydata(i).T = fileread(files);
arraymean(i) = mean(mydata(i));
end
The files that I need to compute are more or less like this:
Delta_X 3.000000 3.000000 3.000000
***End_of_Header***
X_Value C_P1N1 C_P1N2 C_P1N3
0.000000 -0.044945 -0.045145 -0.045705
0.000000 -0.044939 -0.045135 -0.045711
3.000000 -0.044939 -0.045132 -0.045706
6.000000 -0.044938 -0.045135 -0.045702
Your first line results in 'myfiles' being a structure array with components that you will find defined when you type 'help dir'. In particular, the names of all the files are contained in the structure element myfiles(i).name. To display all the file names, type myfiles.name. So far so good. In the for loop you use 'fileread', but fileread (see help fileread) returns the character string rather than the actual values. I have named your prototype .lvm file DinaF.lvm and I have written a very, very simple function to read the data in that file, by skipping the first three lines, then storing the following matrix, assumed to have 4 columns, in an array called T inside the function and arrayT in the main program
Here is a modified script, where a function read_lvm has been included to read your 'model' lvm file.
The '1' in the first line tells Octave that there is more to the script than just the following function: the main program has to be interpreted as well.
1;
function T=read_lvm(filename)
fid = fopen (filename, "r");
%% Skip by first three lines
for lhead=1:3
junk=fgetl(fid);
endfor
%% Read nrow lines of data, quit when file is empty
nrow=0;
while (! feof (fid) )
nrow=nrow + 1;
thisline=fscanf(fid,'%f',4);
T(nrow,1:4)=transpose(thisline);
endwhile
fclose (fid);
endfunction
## main program
myfiles = dir('*.lvm'); % To load every file of .lvm
for i = 1:length(myfiles) % Loop with the number of files
files=myfiles(i).name;
arrayT(i,:,:) = read_lvm(files);
columnmean(i,1:4)=mean(arrayT(i,:,:))
end
Now the tabular values associated with each .lvm file are in the array arrayT and the mean for that data set is in columnmean(i,1:4). If i>1 then columnmean would be an array, with each row containing the files for each lvm file. T
This discussion is getting to be too distant from the initial question. I am happy to continue to help. If you want more help, close this discussion by accepting my answer (click the swish), then ask a new question with a heading like 'How to read .lvm files in Octave'. That way you will get the insights from many more people.
I need to save in the same'file.mat' three matrix M1,M2,M3; with a very test i get one of those matrix, after all tests, each matrix needs to be saved in the same file.
function [M1,M2,M3]= atlet(Numtest)
if (Numtest==1)
%instruction
elseif (Numtest==2)
%instruction
elseif (Numtest==3)
%instruction
endif
save ('alter_test.mat','M1','M2','M3')
endfunction
every time I have just one matrix, however i need to save all of them in alter_test.mat without deleting anyone.
for example when I pass this command:
[M1,M2,M3]= atlet(1)
I got in alter_test.mat the matrix M1
when I pass this command then:
[M1,M2,M3]= atlet(2)
I got in alter_test.mat the matrix M2 and M1 is deleted.
However i need to have both of them M1 and M2.
I would be very gratful If you could help please.
you can try to append to the .mat file , however -append will refresh your indices. you could define a two by two matrix in this case and increase the row every time the .mat file is being called in a loop, this way for example your M1 will be an array of M1 results documented earlier.
to do so first:
1.load the .mat file: load('alter_tst.mat','M1','M2','M3');
2.append your new M1-M3 to the previous ones named MT1, MT2,MT3: M1=[M1;MT1];
3.save('alter_tst.mat','M1','M2','M3','-append');
save(..., 'append')To append to the previous save, then later you can bring the previous save to the workspace and append the new matrix to the old matrix thus creating a 2 X 3 matrix and later n x 3 matrix.