Columnwise '-append'?
10 次查看(过去 30 天)
显示 更早的评论
So, I have two cell arrays, one is double and the other is char. I am trying to export the data using dlmwrite to multiple text files, one for each matrix of the array. However, I need the character data appended columnwise, not rowwise, to its respective numeric data. If I have the wrong approach, please let me know, but here is an example:
Coordinates{i} contains {i} numeric matrices, each sized n-by-21. Each n is different and {i} itself is defined far earlier in my code, so I will use an arbitrary value here.
Times{i} contains {i} character matrices, each sized n-by-24. Each of the n Times corresponds to the nth Coordinate. I cannot alter the format of Times, they have character data because the program I must use to read them will only read this particular format. Yes, I would not have this problem if my Times values were also strictly numeric, but alas they are not.
Here is the code I've been trying to work with:
for i=1:4
fname = (sprintf('OutputFile_%d.txt',i));
fin = Coordinates{i};
time = Times{i};
dlmwrite (fname,fin,'precision','%.12f');
dlmwrite (fname,time,'-append','delimiter','');
end
My delimiter is blank on purpose in the last line, otherwise each character of the Times data will also be separated by the delimiter, which breaks the format.
This code will appropriately create multiple text files, one for each of the ith matrices contained in Coordinates. However! Using the '-append' setting by default will write the Times data to the following empty rows of the files, which is not useful. If I change that last line to this:
dlmwrite (fname,time,'-append','coffset',21,'delimiter','.');
It will successfully shift the Times data to the right 21 columns, but the data will still be shifted downward rowwise as well, even though there is nothing contained in the cells above it anymore. Padding 'coffset' with an even greater value than necessary also doesn't work. Also, it breaks the format of the Times data, because I added a delimiter to check.
I have not had success trying to add a 'roffset' attribute with a negative value to force the data to shift upwards, it just doesn't seem to recognize that.
I have also tried combining the corresponding data beforehand, but then I get warnings about numeric data being truncated, and it seriously messes up my data. I imagine MATLAB doesn't like combining numeric data and character data within the same matrix.
Is there a solution to this, some way to make '-append' function columnwise instead of rowwise -- or some other way to get this to work without resorting to C?
Thank you for reading, I apologize if this was long.
Thanks to Jarrod, I got a Tab-Delimited file out of this:
for i=1:4
fname = (sprintf('OutputFile_%d.txt',i));
fin = Coordinates{i};
time = Times{i};
dlmwrite (fname,colheaders);
for k = 1:size(Coordinates{i},1);
coordstring = '';
timestring = '';
coordString = [coordstring ',' num2str(fin(k,:))];
timeString = [timestring time(k,:)];
stringToAdd = [coordString timeString];
dlmwrite (fname,stringToAdd,'-append','delimiter','');
end
end
0 个评论
采纳的回答
Jarrod Rivituso
2011-4-6
Could you simplify your input by creating the string first using MATLAB code and then use dlmwrite to add a single string? This might make it easier.
For clarity, here is what I'm thinking
for i=1:4
fname = (sprintf('OutputFile_%d.txt',i));
fin = Coordinates{i};
time = Times{i};
fileID = fopen(fname);
for k = 1:n
coordString = '';
for j = 1:21
coordString = [coordString ',' num2str(fin(k,j))];
end
timeString = '';
for j = 1:24
timeString = [timeString time(k,j)];
end
stringToAdd = [coordString timeString];
end
fprintf(fileID,'%s',stringToAdd);
end
fclose(fileID)
end
You can do some fancy indexing to avoid some of those for loops, but I left them there for clarity. The best part about this method is that it is easy to debug cause you can see each string you are about to write.
There may be a way to do it with dlmwrite, but I honestly don't know it (I welcome anyone to set me straight :) )
Hope this helps!
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!