How to import a spesific variable from a matrix in a file?

I have a file "SANJOSE" that has two values in it "20 30"
I would like to use "loc1lat = importdata('SANJOSE')" command to import the first value of the 1x2 matrix, not the whole thing.
I am not sure what syntax to use after ('SANJOSE', ???) to specify which variable in the matrix I want.
Thanks

2 件のコメント

José-Luis
José-Luis 2012 年 9 月 18 日
Is SANJOSE a .mat file?
sono
sono 2012 年 9 月 18 日
編集済み: sono 2012 年 9 月 18 日
No it doesn't have an extension. I use the following script to create the file:
loc1N = input('Enter location 1 NAME: \n','s');
fid = fopen(loc1N,'a');
fprintf(fid, '%f %f\n', loc1lat, loc1long);
fclose(fid);
-----------------
I am trying to use this code to import the data but by calling out the variable that is = to the file name but cant figure out how to get it to work.
%request location names
loc1N = input('Enter NAME of starting location: \n','s');
%import data from database
loc1info = importdata('loc1N');
. . .
importdata('SANJOSE'); works but importdata('loc1N'); when loc1N = SANJOSE does not work for some reason :(
Also would be nice to prompt the user for name and import the data in one step but not critical.

サインインしてコメントする。

 採用された回答

José-Luis
José-Luis 2012 年 9 月 18 日
編集済み: José-Luis 2012 年 9 月 18 日

0 投票

filename = 'SANJOSE';
(SANJOSE is a text file containing one line: 20 30)
Two options, either pass the file name SANJOSE as a string:
fid = fopen('SANJOSE','r'); %'r' means you only want to read the data.
your_value = fscanf(fid,'%d',1); %Will only read the first value
fclose(fid);
Or, pass the variable name (filename) that contains a string:
fid = fopen(filename,'r');
your_alternative_value = fscanf(fid,'%d',1);
fclose(fid);

4 件のコメント

sono
sono 2012 年 9 月 18 日
編集済み: sono 2012 年 9 月 18 日
This seems to work great for getting the first value
fid = fopen(loc1N,'r');
loc1lat = fscanf(fid,'%d',1);
fclose(fid);
But how do I get the second or third? Changing the 1 to a 2 doesn't seem to work:
fid = fopen(loc1N,'r');
loc1lat = fscanf(fid,'%d',2);
fclose(fid);
-----------------
PS: How do you make your code look nice and grey? Also (using firefox) I cant seem to be able to get a fresh line in this window, "single enter" doesn't seem to register in the preview window and "double enter" produces new line x2 effect, more the "double enter" doesn't seem to register either :(
José-Luis
José-Luis 2012 年 9 月 18 日
編集済み: José-Luis 2012 年 9 月 18 日
filename = 'SANJOSE';
fid = fopen('SANJOSE','r');
valsToSkip = 1; %1 if you want the second value, 2 if your want the third, etc...
while (valsToSkip)
fscanf(fid,'%[0-9]'); %This will work only for integers, if you want other numbers '[0-9.eE]' would be my bet.
valsToSkip = valsToSkip - 1;
end
your_second_value = fscanf(fid,'%d',1);
fclose(fid);
fid = fopen(filename,'r');
your_first_value = fscanf(fid,'%d',1);
fclose(fid);
Changing from 1 to 2 does not work as that option defines the number of values you want to read, not their position in the file. I would encourage you to read the documentation for fread, fseek, fscanf. They are low level input/output functions that allow you to do the kind of manipulations your are asking about.
You edit your entry with the buttons on top of the editing windows. To mark as code (grey), you should use the {} Code button.
sono
sono 2012 年 9 月 18 日
編集済み: sono 2012 年 9 月 18 日
The skip variable trick didn't seem to work. Ends up outputting an empty matrix.
loc1long =
[]
yeah I am trying to read up right now but can't seem to find a solid answer for pulling specific variables out of a sequence =/
thanks though :)
José-Luis
José-Luis 2012 年 9 月 18 日
編集済み: José-Luis 2012 年 9 月 18 日
My bad, I had not tested the code I posted above: it would work only if you want to skip one value. Instead you could move in your file character by character until you find a space and then read the next variable.
filename = 'SANJOSE';
fid = fopen('SANJOSE','r');
valsToSkip = 2; %1 if you want the second value, 2 if your want the third, etc...
while (valsToSkip)
currentChar = fscanf(fid,'%c',1);
if currentChar == ' ';
valsToSkip = valsToSkip - 1;
end
end
your_second_value = fscanf(fid,'%d',1);
fclose(fid);
fid = fopen(filename,'r');
your_first_value = fscanf(fid,'%d',1);
fclose(fid);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLarge Files and Big Data についてさらに検索

質問済み:

2012 年 9 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by