dlmread

I've created, by concatenating inputs from the user, a path to files on my computer that contain data I want to read into matlab. Example:
pt = input('Enter number:','s')
run = input('Enter run number','s');
task = input('What task?','s');
run_task = strcat(run,'_',task)
pt_plus_run = strcat(pt,'_',run)
main_path='C:\XXXXX\XXXX_ XXXX\XXX_ files',
path_to_data=strcat(main_path,'\',pt,'\',pt_plus_run,'\',pt_run_task)
data=dlmread(path_to_data,',', [1 2 -1 7])
It is at the dlmread statement that the code chhkes. Can I use a concatentated string variable in dlmread?
Thanks for your help!

1 Comment

Fangjun Jiang
Fangjun Jiang on 22 Nov 2011
Use {}Code format next time.

Sign in to comment.

 Accepted Answer

Fangjun Jiang
Fangjun Jiang on 22 Nov 2011

0 votes

Check the help of dlmread(). I believe the range [R1, C1, R2, C2] won't allow negative number.
use fullfile() instead of strcat() to combine path and file name.

19 Comments

Frank
Frank on 22 Nov 2011
Would you elaborate on the {} code you recommended?
Also, the following chokes at the dlmread: Any suggestions?
pt = input('Enter pt number:','s')
run = input('Enter run number:','s');
task = input('What task?','s');
pt_plus_run = strcat(pt,'_',run)
pt_run_task = strcat(pt,'_',run, '_',task,'.csv')
fullfile_name=fullfile('x:','xxxxx','xxxxxxxx','xxxxxxx',pt,pt_plus_run,pt_run_task)
data=dlmread(fullfile_name,[1 2 -1 7])
Fangjun Jiang
Fangjun Jiang on 22 Nov 2011
The {}Code is a button above your question box for you to apply formatting.
What do you mean by [1 2 -1 7] in the call of dlmread()?
Frank
Frank on 22 Nov 2011
I still don't understand what you mean by {}Code
Regarding [1 2 -1 7] I'm referring to the third ( zeroth being the first) through eigth column from the second row to the bottom of the column
Fangjun Jiang
Fangjun Jiang on 22 Nov 2011
I don't see anywhere that the range can be a negative number.
Frank
Frank on 22 Nov 2011
Try it. It works for me to read to the bottom of the columns.
Fangjun Jiang
Fangjun Jiang on 22 Nov 2011
Okay, thank you! I learned a trick today!
What is the error or problem you are having then? Did you check fullfile_name is correct?
Fangjun Jiang
Fangjun Jiang on 22 Nov 2011
You forgot to specify the delimiter in the code in your comment.
Frank
Frank on 22 Nov 2011
It chokes at the dlmread comment:
Here is the error message:
??? Error using ==> dlmread at 62
The file 'E:\xxxx\xxxx\xxxx\_\__.csv' could not be
opened because: No such file or directory
Error in ==> Concatenate at 15
data=dlmread(fullfile_name)
Fangjun Jiang
Fangjun Jiang on 22 Nov 2011
All right, then check the full path and name of the file. exist(fullfile_name,'file')
Frank
Frank on 22 Nov 2011
OK, I'm working through it.
Frank
Frank on 22 Nov 2011
Thank you for your direction.
Frank
Frank on 23 Nov 2011
OK, so the path doesn't exist what do I do? What is wrong with this script?
pt = input('Enter pt number (001 to 010):','s')
run = input('Enter the run number (R0 to R3):','s');
task = input('What task? ','s');
pt_plus_run = strcat(pt,'_',run)
pt_run_task = strcat(pt,'_',run, '_',task,'.csv')
%main_path='E:\BK\acd_ Study\cd_Study_ CSV_ files'
%path_to_data=strcat(main_path,'\',pt,'\',pt_plus_run,'\',pt_run_task)
fullfile_name=fullfile('E:','BK','acd_Study','cd_Study_CSV_files',pt,pt_plus_run,pt_run_task)
exist('fullfile_name','file')
Fangjun Jiang
Fangjun Jiang on 23 Nov 2011
I don't know. Just run your code line by line to see where is the problem. You know your file name convention. You know your folder structure. Just try to match the string with the actual file. I usually use fullfile this way, for example
PathStr='c:\mydocument\mydata';
FileName='mydata.txt';
fullfile(PathStr,FileName);
But nothing is wrong using it like you did.
Frank
Frank on 23 Nov 2011
The code "works" until the last time, when exist gives me a zero!
I'll tinker some more. If tomorrow is a holiday for you, enjoy it!
Frank
Frank on 29 Nov 2011
Progress has been slooooooooow: Here is what I've got and the resultant errors:
pt = input('Enter pt number (001 to 010):','s');
run = input('Enter the run number (R0 to R3):','s');
task = input('What task? Choose from ar, ae, hb, ct, dr, ds, or am):','s');
upper_or_lower= input ('Upper (enter BD_UE) or lower (enter BE_LE)?','s');
pt_plus_run = strcat(pt,'_',run);
pt_run_task = strcat(pt,'_', task, '_', run, '_', upper_or_lower, '.csv');
whole_sensor_data_file_path= strcat('E:\Bck\aCl_Study\Cl_CSV_files','\', pt,'\', pt_plus_run,'\', pt_run_task)
exist('whole_sensor_data_file_path','file')
PathStr=whole_sensor_data_file_path
FileName=pt_run_task
f= fullfile(PathStr,FileName)
dlmread('f', [1 2 -1 7])
OUTPUT and ERRORS:
ans =
0
PathStr =
E:\Bck\aCl_Study\Cl_CSV_files\001\001_R0\001_ar_R0_BD_UE.csv
FileName =
001_ar_R0_BD_UE.csv
f =
E:\Bck\aCl_Study\Cl_CSV_files\001\001_R0\001_ar_R0_BD_UE.csv\001_ar_R0_BD_UE.csv
??? Error using ==> sprintf
Invalid format.
Error in ==> dlmread at 72
delimiter = sprintf(delimiter); % Interpret \t (if necessary)
Error in ==> test_pad at 15
dlmread('f', [1 2 -1 7])
Any help and/or ideas are appreciated!!!
Fangjun Jiang
Fangjun Jiang on 29 Nov 2011
No. f is already a string. use dlmread(f, [1 2 -1 7])
Frank
Frank on 29 Nov 2011
??? Error using ==> sprintf
These are the errors I get using dlmread(f, [1 2 -1 7])
Invalid format.
Error in ==> dlmread at 72
delimiter = sprintf(delimiter); % Interpret \t (if necessary)
Error in ==> Concatenate at 18
dlmread(f, [1 2 -1 7])
Frank
Frank on 29 Nov 2011
Does anybody have any idea what the heck is wrong with my dlmread--that is where it chokes!!!!!
Walter Roberson
Walter Roberson on 29 Nov 2011
exist('whole_sensor_data_file_path','file')
should be
exist(whole_sensor_data_file_path,'file')
The whole_sensor_data_file_path you construct includes pt_run_task so it includes .csv at the end. But then you assign whole_sensor_data_file_path to PathStr and you assign pt_run_task to FileName and then you fullfile() together the combination of the two. That is going to add the file name string complete with .csv on to the end of a string that already includes all of that. The name that gets constructed will not be something you can open .

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!