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

 Respuesta aceptada

dpb
dpb el 5 de Oct. de 2014

0 votos

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 comentarios

Davin
Davin el 5 de Oct. de 2014
Thanks dpb. I have tried what you wrote above, but its still not working. Let me explain a bit more, I have a listbox with are a list of sectors, and the series above correspond to my listbox values, thats why i use the if command. I used a for loop to look into the series that correspond to my selectedItem, so for example, selectedItem 1 corresponds to advertising, I enter the loop, but only once as the selectedItem = 1. and it goes out after. Entering the loop, i do get my ID bust textscan does not give me anything. From what I can understand, assigning i to ticker will put in relation the ticker with the loop, which i dont find logical as for me the textscan just scans all the txts in the fileid and put is assigned to column 1 in sector = ticker{1}.
My file is a .lst but its a normal txt file from notepad. my code is the following, i did tried the above, it works only when the file is explicitly put in the fopen. but when its in a loop, it does not, even with a fileid which seems correct.. There is my txt file attached.
series = { 'Advertising.txt', 'AeroDefense.lst'}
for i=1:numel(series)
if selectedItem1 == i
%open file
fid = fopen('series{i}', 'r')
ticker= textscan(fid,'%s')
fid = fclose(fid)
sector = ticker{1}
end
end
I expected to get 2 tickers in sector. but I have empty cell.
Thanks again for fclose and your reply.
dpb
dpb el 5 de Oct. de 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 el 5 de Oct. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 5 de Oct. de 2014

Comentada:

el 5 de Oct. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by