I need to write two different matrices in a file. However, first matrix is just initial values=> 25*ones(10,10). Second matrix is also 10x10 matrix , and updating in every iteration. My question is , first matrix will be on the beginning of the file, later on second matrix will be appending the end of the first matrix after each iteration and updating. I dont want to overwrite the second matrix everytime, which is happening when i run these codes
My codes are like this:
if ss==5000;
dlmwrite('d:\Temp.txt',Tin*ones(10,10), ' ');
dlmwrite('d:\Temp.txt', Tnew,'-append','roffset', 1, 'delimiter', ' ');
ss=0
end
Could you help me on this ?
Thanks in advance.
I've put together a complete example that demonstrates what I think you want.
If the file doesn't exist, it creates it and puts your first matrix inside it. If it does exist, it appends subsequent data
filename = 'd:\Temp.txt';
for ss=1:25000
if mod(ss,5000)==0
%generate some data to write
Tnew = rand(10,10);
if ~exist(filename,'file')
dlmwrite(filename,ones(10,10), ' ');
end
dlmwrite(filename, Tnew,'-append','roffset', 1, 'delimiter', ' ');
end
end
Related
I am appending to a csv file. The current code outputs a line: a;b;c;d
I need it to output a;b;c;d;
notice the extra ';' at the end of d. That is essential
matrix = [a,b,c,d]
dlmwrite('matrix.csv', matrix, 'delimiter',';','-append','roffset',0, 'precision',14)
any help would be appreciated.
I have had to keep variables a,b,c and d as numbers, or it makes it a character vector (or something) which makes my csv look funny
I've always had problems with the MatLab inbuild CSV writing methods. Why don't you code your own .CSV writing method?
Here, you could make a function something like:
function write_to_csv(filepath, matrix)
csv = fopen(filepath, 'a+'); % check what sort of open you'd like : https://uk.mathworks.com/help/matlab/ref/fopen.html#inputarg_permission
for ii = 1 : numel(matrix) % this loop depends on the dimensions of your matrix
fprintf(csv, '%s;', matrix(ii)); % check fprintf return type, depending on the data in the matrix : https://uk.mathworks.com/help/matlab/ref/fprintf.html?searchHighlight=fprintf&s_tid=doc_srchtitle#inputarg_formatSpec
end
fclose(csv);
end
This works for a 1D matrix you've supplied, run with:
write_to_csv('matrix.csv',matrix)
I want to write a function that takes number n as input, then outputs a tab separated word document that looks like 5 rows of:
1 2 3...n n n-1 n-2 ..1
Let me tell you what I have tried already: It is easy to create a vector like this with the integers I want, but if I save a file in an ascii format, in the output the integers come out in a format like " 1.0000000e+00".
Now I googled to find that the output can be formatted using %d and fprintf, but given the row length is part of the input, what would be the most efficient way to achieve it?
maybe something like this:
Nrow = 5;
N = 10;
dlmwrite('my_filename.txt', repmat([1:N, N:-1:1], Nrow, 1), 'delimiter', '\t', 'precision', '%d');
If you mean a normal *.txt kind of file, I would normally use a for loop with fprintf(fileid,'%d things to print',5), with the appropriate fopen(.) statement. You'd be surprised what a good job fopen with 'w' and 'a' does. Try it and let us know!
In response to rayryeng: You are right! Here is a sample of code for writing a matrix to file using fprintf, without a for-loop.
A=rand(5);
fid=fopen('Rand_mat.txt','w');
fprintf(fid,'%0.4f %0.4f %0.4f %0.4f %0.4f\n',A');
fclose (fid);
where A is transposed because MATLAB reads the columns of the matrix first.
Thanks!
I am trying to write a matrix into a text file using matlab. I used the code shown below
fName = 'Audio.txt';
fid = fopen('Audio.txt','w');
dlmwrite('Audio.txt', a, '\n');
but i am not getting all the values in single line.What I am expecting is one element in every line.How to make it happen
I am getting error for my below code: temp=reshape(img',irow*icol,1);
Error message:Error using '
Transpose on ND array is not defined.
What is solution for this. I think I have to use permute(A,order) command. But I dont know how to use this command in my code. Do you know any solution?
for i=1:M
str=strcat(int2str(i),'.jpg'); %concatenates two strings that form the name of the image
eval('img=imread(str);');
subplot(ceil(sqrt(M)),ceil(sqrt(M)),i)
imshow(img)
if i==3
title('Training set','fontsize',18)
end
drawnow;
[irow icol]=size(img); % get the number of rows (N1) and columns (N2)
temp=reshape(img',irow*icol,1); %creates a (N1*N2)x1 matrix
S=[S temp]; %X is a N1*N2xM matrix after finishing the sequence
%this is our S
end
I assume the code was designed for grey scale images. For matrices with more than two dimensions, you have to use permute. One solution could be:
[irow icol d]=size(img);
temp=reshape(permute(img,[2,1,3]),[irow*icol,d]);
Which results in a nx3 matrix, each column corresponding to one colour. You have to change the last line as well, but I don't know what you are expecting. Maybe take a look at cat
clc
clear all
ii=1;
S =cell(size(30,1)); % cell size.
for ii=1:1:3
rand_id= rand(1,1) *3; % Randomly generte a number between 1 to 3.
if (rand_id<1)
rand_id=1; % 0 is ommitted.
else rand_id=floor(rand_id);
end
% rand_id will be used to open a previously saved file randomly.
if (rand_id==1)
f_id_1=fopen('C1.txt','r'); % Open and read a file.
elseif (rand_id==2)
f_id_1=fopen('C2.txt','r'); % Open and read a file.
end
% saning the file to read the text.
events_1=textscan(f_id_1, '%s', 'Delimiter', '\n');
fclose(f_id_1);
events_1=events_1{1}; % saving the text.
rand_event=events_1{randi(numel(events_1))}; % selects one text randomly.
S{ii}=rand_event;
end
I wrote the above code to randomly select a file. The file contains number of sentences. My aim is to randomly pick a sentence . I did that. Now, my problem is I cant save all the picked sentences inside the loop.
When I declare S(ii)=rand_event It shows error. When I try S(ii)=rand_event(ii) It only returns 1, 2, 3 characters in the three loops.
Please help.
S(ii)
is considered to be a matrix with well defined dimensions. I guess that your 'sentences' have different length. One solution might be to use a cell array.
S{ii}=rand_event
Cell arrays use curly braces.