Matlab unable to format string to path

3 ビュー (過去 30 日間)
Sajjid Wajid
Sajjid Wajid 2022 年 1 月 6 日
編集済み: Stephen23 2022 年 1 月 6 日
In original script there is part of code where is this
filepathsource='C:\Users\...';
strFP=dir([filepathsource '*.txt']);
Which works nicely. Problem is that i need to change that filepathsource to some more dynamic so i did.
filepathsource=string(strcat(app.folderPath,'\',string(app.currentCount),'\'));
strFP=dir([filepathsource '
*.txt']);
But i end with error
Error using dir
Name must be a text scalar.
And i need original formating because i get errors later in script. Is there any way how can i fix it? I tried everything but always is something somewhere wrong. And i just dont understand whats difference between original filepathsource and the one that i created bot are strings. (output of my filepathsource is correct when i display it)
  1 件のコメント
Stephen23
Stephen23 2022 年 1 月 6 日
編集済み: Stephen23 2022 年 1 月 6 日
" Is there any way how can i fix it? "
Yes: use FULLFILE instead of concatenating strings and separators.

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 1 月 6 日
Use fullfile() instead of concatenation of characters or strings.
Your most immediate problem is that the results of
['characters' "string"]
is not 'charactersstring' or "charactersstring" and is instead ["characters" "string"] -- that is, [] between a character vector and a string is done by converting the character vector into a string object and then you have [] of two string objects which gives you a string array.
To join a character vector and a string object use strjoin() or use the + operator such as 'hello' + "there"
But all this is best avoided by using fullfile()

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by