Why does ls add trailing space preventing output use as filename string?

조회 수: 5 (최근 30일)
Please consider the following error and resolution. What should I be doing differently? (Summary: ls is adding a trailing space to the end of the filepath and I don't know why.)
Erroneous code:
DICOMdatafolder = '/home/sony/Documents/research/data/DICOMfiles/5';
foldername = FindRTPlanDICOMFile(DICOMdatafolder);
planinfo = dicominfo(ls(fullfile(DICOMdatafolder,foldername,'*dcm')))
Error using dicom_getFileDetails (line 14)
File "/home/sony/Documents/research/data/DICOMfiles/5/DOE^JOHN_[longname]/2[longname].dcm
" not found.
Error in dicominfo (line 55)
fileDetails = dicom_getFileDetails(filename);
Error in BlurPortalDose (line 31)
planinfo = dicominfo(ls(fullfile(DICOMdatafolder,foldername,'*dcm')))
Error workaround:
DICOMdatafolder = '/home/sony/Documents/research/data/DICOMfiles/5';
foldername = FindRTPlanDICOMFile(DICOMdatafolder);
filename = ls(fullfile(DICOMdatafolder,foldername,'*dcm')); filename(end)=[];
planinfo = dicominfo(filename)
planinfo =
struct with fields:
Filename: '[too long; I hope MIM will read this and fix their filename length]'
FileModDate: '30-Jan-2018 18:41:34'
FileSize: 39586
Format: 'DICOM'
[...]

채택된 답변

Jan
Jan 2018년 1월 31일
ls is thought to display the contents of the directory in the command window. Use dir instead to construct a file name:
FileDir = dir(fullfile(DICOMdatafolder, foldername, '*dcm'));
File = fullfile(FileDir(1).folder, FileDir(1).name);
  댓글 수: 4
Stephen23
Stephen23 2018년 1월 31일
편집: Stephen23 2018년 1월 31일
"Should the ls documentation be modified to clarify ls is intended only for displaying contents in the command window? It only says, for example, "lists the contents of the current folder" which is not substantially different from dir's "lists files and folders in the current folder."'
The ls documentation already recommends "Use the dir command to return file attributes for each file and folder in the output argument", and has a link to dir. The ls description is "lists the contents of the current folder": its output may be useful in some situations, and it is not the job of any language to tell its users what to do or not to do: it is up to the programmer to read the documentation and understand the tools that they are using.
For processing sequences of files the MATLAB documentation only has examples using dir, e.g.:
Daniel Bridges
Daniel Bridges 2018년 1월 31일
Indeed! Yet placing it under the heading 'Alternatives' suggests that it is an option rather than as an imperative or recommendation.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2018년 1월 31일
ls is not adding a trailing space. ls is adding a trailing linefeed.
On Mac and Linux, ls() calls the system ls executable, which outputs a multicolumn text stream with embedded newlines. You should avoid using ls to get file names. You cannot just split the result of ls at whitespace because filenames can contain whitespace.
If you need a filename for processing you should be using dir()

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by