Dynamic filepath for sftp not allowed

조회 수: 3 (최근 30일)
Ben Cotts
Ben Cotts 2022년 3월 16일
편집: Adam Danz 2025년 6월 17일
Home="/folder1/folder2";
Scanname="Exp04_Sample1";
Sftp_path=strcat(Home,"/",Scanname);
fileInfo=dir(s,Sftp_path);
Why does the above not work?
But
fileInfo=dir(s,"/folder1/folder2/Exp04_Sample1");
will work?
  댓글 수: 1
Manas Shivakumar
Manas Shivakumar 2023년 10월 9일
Can you describe the variable 's' ? specifically its purpose and its type. I couldn't find MATLAB's dir() function supporting a signature like this dir(var_a, var_b). List folder contents - MATLAB dir - MathWorks India

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

채택된 답변

Adam Danz
Adam Danz 2025년 6월 16일
편집: Adam Danz 2025년 6월 17일
The question is refering to the dir function for SFTP or FTP servers.
I see no reason why the OP's two versions should not behave the same. If this happens again, please report it to tech support.
Home="/folder1/folder2";
Scanname="Exp04_Sample1";
Sftp_path=strcat(Home,"/",Scanname);
full = "/folder1/folder2/Exp04_Sample1";
isequal(Sftp_path, full)
ans = logical
1
Note that using fullfile is better than manually constructing file paths.
Sftp_path = fullfile(Home,Scanname)
Sftp_path = "/folder1/folder2/Exp04_Sample1"

추가 답변 (1개)

Vatsal
Vatsal 2023년 11월 17일
Hi,
The issue seems to be with the 'dir' function. The 'dir' function in MATLAB is used to list files and folders in a directory. When you use 'dir' with a variable as an argument, MATLAB expects the variable to be a string that specifies the name of the file or folder. If the variable is not a string or if the string does not represent a valid file or folder name, 'dir' will not work as expected. In the provided code, 's' and 'Sftp_path' are variables. If 's' is not defined in your code or does not represent a valid file or folder name, the 'dir' function will not work. Similarly, if 'Sftp_path' does not represent a valid file or folder name, the 'dir' function will not work.
I have attached a modified code below:
Home = '/folder1/folder2';
Scanname = 'Exp04_Sample1';
Sftp_path = fullfile(Home, Scanname);
fileInfo = dir(Sftp_path);
In this code, 'fullfile' is used instead of 'strcat' to construct the file path. 'fullfile' constructs a full file specification from the specified folder and file names. It is generally a better choice for constructing file paths because it automatically handles the file separators.
To learn more about the usage and syntax of 'dir' and 'fullfile', you may refer to the MathWorks documentation links below:
I hope this helps!

카테고리

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

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by