Writing to file with loops and loop error

3 views (last 30 days)
Muneer
Muneer on 28 Feb 2014
Edited: per isakson on 4 Mar 2014
Hello, Here is my current code:
clc;
clear;
clear segarray;
block_size = 10000;
filename = 'brktorque-vel-navgraph.txt';
FormatString = ['%*s %s %s %s %s %s %s'];
fid = fopen(filename);
cellchunk = textscan(fid,FormatString,1,'delimiter','\t');
while ~feof(fid)
tic
segarray = textscan(fid, FormatString, block_size, 'delimiter',char(9));
for i = 1 : 6(segarray);
segarray{i} = str2double(segarray{i}) ;
isn = isnan(segarray{i}) ;
segarray{i}(isn) = 0 ;
end
toc
[b, ~, n] = unique(segarray(:,6) , 'stable');
firstColumn = accumarray(n , segarray(:,4) , size(b) , @(x) min(x));
secondColumn = accumarray(n , segarray(:,5) , size(b) , @(x) max(x));
thirdColumn = accumarray(n , segarray(:,3) , size(b) , @(x) mode(x));
outputArray = cat(2 , firstColumn , secondColumn , thirdColumn, b);
csvwrite('brktorque-vel-navgraph-agg.csv',outputArray)
end
fclose(fid);
I have two questions, hopefully you all can provide some assistance: 1. I'm having trouble in the second iteration of my loop with the unique function. The error is "Error using cell/unique (line 86) Input A must be a cell array of strings." I'm not sure why this is happening. 2. I'm worried that my loop will overwrite what is being written to the file in each iteration. I actually want the output of each iteration to be saved after the previous output and all in one file. Am I doing this correctly?
Any help would be greatly appreciated. Thanks in advance!
  2 Comments
Jan
Jan on 28 Feb 2014
What does this mean:
for i = 1 : 6(segarray);
This is no valid Matlab syntax and inconsequence the code should not run at all.
Muneer
Muneer on 4 Mar 2014
Hello Jan,
I pull in 6 columns from my file and use that for loop to go through each of the 6 columns and replace null vals, etc. Is there a better way to go about this? Thanks.

Sign in to comment.

Answers (1)

per isakson
per isakson on 28 Feb 2014
1. Probably segarray(:,6) is empty
>> unique({[]})
Error using cell/unique (line 86)
Input A must be a cell array of strings.
2. csvwrite has no option to append to file. You need to use a function with an append-option, e.g. fopen together with fprintf
Why the while loop?
.
  2 Comments
Muneer
Muneer on 4 Mar 2014
The while loop is to pull in 10000 rows at a time until I get to the end of the file. Can this be done in a faster way?
I will try fopen. Thanks for your help!
per isakson
per isakson on 4 Mar 2014
Edited: per isakson on 4 Mar 2014
You have not described the goal and your constraints well enough.
  • How large is the text file?
  • How much physical memory (ram) is it in the computer?
  • Is speed a real problem?
  • Is the format of the file known? What is the purpose of cellchunk
"[...] in a faster way?" Yes, most likely.
  • Use the largest possible value of blocksize that memory allows
  • Read and convert to numerical in one step with textscan

Sign in to comment.

Categories

Find more on Characters and Strings 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!