If states had no space in their names, or if you had commas as delimiters in the CSV file, you could go for a variant of
[state, temp] = textread('myFile.csv', '%s %d', 'delimiter', ',', ...
'headerlines', 1) ;
Now as it seems that there are spaces in names and no comma as delimiter, you can read line by line and extract states and temperatures with more specific functions (TEXTSCAN, FSCANF, FGETL+SSCANF, etc) and based on position if needed (e.g. start reading temperatures from char. 15 on). Then you can use STRCMPI to find indices of relevant states, and get corresponding temperatures based on these indices. But you could also go for a solution based on regular expressions (less common approach for this kind of structured data), that I illustrate below:
>> buffer = fileread('myFile.csv') ;
>> state = 'New York' ;
>> temp = str2double(regexpi(buffer, sprintf('(?<=%s\\s*)\\d*', state), ...
'match'))
temp =
83 55
>> state = 'California' ;
>> temp = str2double(regexpi(buffer, sprintf('(?<=%s\\s*)\\d*', state), ...
'match'))
temp =
80 92
Note that I wrote this in a concise manner, but we do the following in fact:
>> pattern = sprintf('(?<=%s\\s*)\\d*', state) ;
>> match = regexpi(buffer, pattern, 'match') ;
>> temp = str2double(match) ;
If you look at the pattern for New Work:
>> pattern
pattern =
(?<=New York\s*)\d*
It tells regexp to match
- As many numeric characters as possible: \d*
- Preceded by (positive look behind (?<=)) the literal New York followed by as many white spaces as possible: New York\s*
Best Answer