Textscan inside a loop

1 view (last 30 days)
Davin
Davin on 5 Oct 2014
Commented: Davin on 5 Oct 2014
Hello,
I am having some difficulties retrieving the info inside some text file i have created, in which there are stock tickers, 3 letters like this :
CCL
EXPE
MAR
so my code is the following :
series = { 'Advertising.lst','AeroDefense.lst','AgriProd.lst'}
for i=1:numel(series)
if selectedItem1 == i
%open file
series{i}
fileid = fopen('series{i}', 'r')
ticker = textscan(fileid,'%s')
fclose =(fileid)
sector = ticker{1}
end
end
The code i think is correctly retrieving the fileid of the series{1} but then when the ticker line is being processed, I get an empty matrix, when the file which is scanned has 3 stock quotes.Normally i should have the sector vector with 3 codes.
Is there an issue in the code.
Thank you very much
Davin

Accepted Answer

dpb
dpb on 5 Oct 2014
Presuming your file is properly formatted, should work just fine excepting you're overwriting the ticker cell array each pass thru the loop so you'll only see the last of the three sets (again presuming all three exist and are properly formed).
To close the file use
fileID=fclose(fileID);
however, your code above has aliased the builtin fclose function with a new variable of the same name.
>> fid=fopen('davin.txt');
>> ticker=textscan(fid,'%s');
>> ticker{:}
ans =
'CCL'
'EXPE'
'MAR'
>> fid=fclose(fid);
>>
The above will work ok if you put the processing for each one of the three lists inside the loop prior to reading the next; otherwise you need to allocate and populate a cell array for each list.
...
ticker(i)=textscan(fid,'%s');
...
You then use the nested addressing -- if were to put the above into the third element for example by
>> ticker(3)=textscan(fid,'%s');
>> ticker{3} % the cell contents
ans =
'CCL'
'EXPE'
'MAR'
>> ticker{3}(1) % the first element of the cell
ans =
'CCL'
  3 Comments
dpb
dpb on 5 Oct 2014
fid = fopen('series{i}', 'r');
You've enclosed the variable name in single quotes thus turning it into a literal text string. So, fopen is trying to open a nonexistent file named series(i).
Use
fid = fopen(series{i}, 'r');
and joy should ensue...
NB: the "curlies" {} around the subscript for the series cell array to dereference the cellstring content and return a character string that fopen requires as it doesn't understand cell strings.
doc strings
for the starting point of how Matlab treats character arrays and then follow the links therein to cell strings for the alternative storage form. Cell strings have the advantage that each cell holds the full character array and returns it with a single subscript whereas one has to use 2D indexing to get the full row of a character array; otherwise one only gets the individual character by itself. But, as shown above, if you try
>> series = { 'davin.txt', 'AeroDefense.lst'};
>> fid=fopen(series(1),'r')
Error using fopen
First input must be a file name of type char, or a file identifier of type double.
>>
you run into the problem of not being a character variable passed to fopen but the cell string instead. The {} "dereference" the content of the cell array to it's underlying data directly. char does the same thing.
The problem with your loop construct is that you really don't need a loop at all as you've recast it above--
series = { 'Advertising.txt', 'AeroDefense.lst'}
fname=series(selectedItem1);
fid = fopen(char(fname), 'r');
...
Above I used and intermediate variable for the filename but you could fold it all directly into the fopen statement if desired...
fid = fopen(series{selectedItem1}, 'r');
...
Again, NB the use of char and/or {} to get the cell content inside the fopen call...
Davin
Davin on 5 Oct 2014
Its exactly that!! Indeed, as i am still on the learning curve of Matlab, I didnt know about the deferencing of {}. Thank you very much.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion 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!