显示 更早的评论
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 个评论
Use {}Code format next time.
采纳的回答
Fangjun Jiang
2011-11-22
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 个评论
Frank
2011-11-22
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])
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
2011-11-22
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
I don't see anywhere that the range can be a negative number.
Frank
2011-11-22
Try it. It works for me to read to the bottom of the columns.
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?
You forgot to specify the delimiter in the code in your comment.
Frank
2011-11-22
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)
All right, then check the full path and name of the file. exist(fullfile_name,'file')
Frank
2011-11-22
OK, I'm working through it.
Frank
2011-11-22
Thank you for your direction.
Frank
2011-11-23
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')
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
2011-11-23
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
2011-11-29
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!!!
No. f is already a string. use dlmread(f, [1 2 -1 7])
Frank
2011-11-29
??? 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
2011-11-29
Does anybody have any idea what the heck is wrong with my dlmread--that is where it chokes!!!!!
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 .
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Text Files 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
