problem in strcat
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello,
%I use the below sample of huge txt file:
ETE 04/01/2010 10145959 18.31 500 Big Cap
ETE 04/01/2010 10150000 18.01 70 Big Cap
ETE 04/01/2010 10170000 18.54 430 Big Cap
ABC 04/01/2010 10190000 18.34 200 Big Cap
%Then, I use the below codes to create a new binary column 'TRADE' with values 'Buy' or 'Sale'.
% Read stock data from file fid = fopen('stocks.txt');
data = textscan(fid,'%s%s%f%f%f%[^\n]','delimiter',' ','headerlines',1);
% Read as text (for later writing)
frewind(fid);
txt = textscan(fid,'%s','delimiter','\n');
fclose(fid);
% Get prices from imported data
Price = data{4};
% Determine which stocks to buy
buy = [true;diff(Price)>=0];
idx = find(~diff(Price));
buy(idx+1) = buy(idx);
% Make string of trade decision
buysell = cellstr(repmat(' Sell',size(Price)));
buysell(buy) = {' Buy'};
% Open file for writing fid = fopen('stocks2.txt','wt');
% Make output string by appending trade decision outstr = strcat(txt{1},[' Trade';buysell]);
% Write out fprintf(fid,'%s\n',outstr{:}); fclose(fid);
% When I apply these codes to the sample file (53 MB) it works fine, but when I apply this to the original file (540 MB) there is the below error message:
outstr = strcat(txt{1},[' Trade';buysell]);
??? Error using ==> cell.strcat at 50
All the inputs must be the same size or scalars.
Could anyone help on what the above pertains to, and how can I solve this problem?
Many thanks in advance,
Panos
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 17 Apr. 2011
I have edited your tags and title because your problem had nothing to do with "outstr", which is merely the name of the variable you happened to be assigning the result to.
Inputs can be combinations of single strings, strings in scalar cells, character arrays with the same number of rows, and same-sized cell arrays of strings.
So your problem is that txt{1} does not have the same number of rows as the [' Trade';buysell] expression does. This is because you are adding a header line for that portion, but you do not add a corresponding header (even an empty string) to the first part.
3 Kommentare
Walter Roberson
am 18 Apr. 2011
You need to put the header on the other part, the txt{1} part:
strcat( [{'Stock'};txt{1}], [' Trade';buysell] )
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!