Filter löschen
Filter löschen

problem in strcat

9 Ansichten (letzte 30 Tage)
Pap
Pap am 17 Apr. 2011
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

Akzeptierte Antwort

Walter Roberson
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.
You need to refer to the strcat documentation
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
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] )
Pap
Pap am 18 Apr. 2011
Thanks for the response Walter,
You are right. I examined the values and the difference is not just a row.
The 'txt' is a '9955028x1' and the 'Price', and as such the 'buysell' are
a '9955011x1', so the header is not probably the case. Also this works fine in a smaller sample.
Can you see any other source of the problem?
Thanks again,
Panos

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by