MATLAB Answers

Ed Seger
0

fopen() in R2008 does not use variables?

Ed Seger
さんによって質問されました 2019 年 10 月 9 日
最新アクティビティ Ed Seger
さんによって 回答されました 2019 年 10 月 14 日
I am trying to open a series of text files and I am getting the 'Invalid filename' error. This is the code:
[FName,PathName,FilterIndex] = uigetfile('C:\temp\matlab\data','MultiSelect','on');
% read in input file
% the input data is one col of floats in text
for f = 1:length(FName)
filename = strcat('C:\temp\matlab\data\', FName(f))
fid = fopen(filename,'rt');
data = fscanf(fid, '%f', 90000 );
fclose(fid);
....
This is what I get for filename:
filename =
'C:\temp\matlab\data\sample0.txt'
I did some debugging and I can open the files one by one if I put the string literal as the first argument to fopen().
Questions:
Does this function only take string literals in R2008?
If so, is there a workaround?
Using MATLAB Version 7.6.0.324 (R2008a)
Thanks,
Ed

  0 件のコメント

サインイン to comment.

タグ

製品


リリース

R2008a

3 件の回答

Steven Lord
回答者: Steven Lord
2019 年 10 月 9 日
 採用された回答

The FName output in your uigetfile call is a cell array. When you use parentheses to index into a cell array the result is also a cell array. Calling strcat on a cell array and a char array results in a cell array. fopen does not accept a cell array as its first input.
Instead use curly braces to extract the contents of the cell in FName, which will be a char array. Calling strcat on a char array and a char array results in a char array that fopen can accept.
sample = {'abc'; 'def'; 'ghi'}
xCell = sample(1)
xChar = sample{1}
yCell = strcat(xCell, '123')
yChar = strcat(xChar, '123')
whos

  0 件のコメント

サインイン to comment.


回答者: Roshan Chavan 2019 年 10 月 9 日
編集済み: Roshan Chavan 2019 年 10 月 10 日

FName(f) is a cell. Convert it to a character array by using char(Fname(f)).
[FName,PathName,FilterIndex] = uigetfile('C:\temp\matlab\data\','MultiSelect','on');
% read in input file
% the input data is one col of floats in text
for f = 1:length(FName)
fid = fopen(strcat(PathName,char(FName(f))),'rt');
data = fscanf(fid, '%f', 90000 );
fclose(fid);
....

  0 件のコメント

サインイン to comment.


Ed Seger
回答者: Ed Seger
2019 年 10 月 14 日

Thanks for the responses, these solved the problem. This focuses on a complaint that I have had about matlab documentation for years, they dont show the type of the arguments in the built-in functions in the help.
Ed

  0 件のコメント

サインイン to comment.



Translated by