Writing to file with loops and loop error

3 visualizzazioni (ultimi 30 giorni)
Muneer
Muneer il 28 Feb 2014
Modificato: per isakson il 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 Commenti
Jan
Jan il 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 il 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.

Accedi per commentare.

Risposte (1)

per isakson
per isakson il 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 Commenti
Muneer
Muneer il 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 il 4 Mar 2014
Modificato: per isakson il 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

Accedi per commentare.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by