what is error in this code?? {W/ textscan input)

5 次查看(过去 30 天)
str = 'GAMMA=000 KP=123'; ;%lambda=222)
A = importdata('nmos.cir','\n');
[row , col] = size(A) ;
cmp = strread (str , '%s ' ) ;
[row2 , col2] = size ( cmp ) ;
for r = 1 : 1
first = textscan (cmp{r},'%s') ;
D = first{1,1};
[var1,val1 ] = strtok(D , '=' );
for j = 1:row
name = textscan(A{j} , '%s ');
B = name{1,1};
[row1 , col1 ] = size(B);
for k = 1 : row1
third= textscan(B{k},'%s') ;
C = third{1,1};
[var2,val2] = strtok(C , '=' );
if (strcmp(var2 , var1))
val2 = val1 ;
end
second = strcat(var2,val2) ;
name(k) =second ;
end
A{j} = name ;
end
end
Whenever 'r' is greater than 1 it gives error
"Error using textscan
First input must be a valid file-id or non-empty character vector.
Error in change_value (line 16) name = textscan(A{j} , '%s '); "

回答(1 个)

dpb
dpb 2017-7-15
Note the specific text of the error message and the line it error'ed on:
"First input must be a valid file-id or non-empty character vector." (emphasis added)
"Error in change_value (line 16) name = textscan(A{j} , '%s '); "
Since A is a cellstr array from the importdata line, not a FID, and you note it does not error when r = 1, then the conclusion to be drawn is that the second element of A is empty.
Set a breakpoint at the line and inspect the content of A and all will likely be revealed.
As an aside to the actual question regarding the specific error and code, this looks like an awfully convoluted way to read a file -- why not just use textscan on the file itself?
Show us a portion of the file you're trying to read; it's probable there's a quite simpler route to the desired end than you're taking here.
  7 个评论
dpb
dpb 2017-7-15
"... from R2017a, ... importdata() returns string objects instead of cell array of string [T]hat can lead to code suddenly stopping working."
I'd expected when I'd asked before if the string class didn't cause some issues wrt cellstr that something like that would be the case.
dpb
dpb 2017-7-15
编辑:dpb 2017-7-17
"...find the required parameters e.g. lambda or GAMMA, replace it with user given value and then re-write the .cir file"
I'm still thinking that could be much simpler by simple string substitution within the array, couldn't it? If you're simply doing a value substitution, that ought to be fairly straightforward within a cell string (or string array altho I don't have the here to test/work with) then rewrite the modfified file from (updated) memory copy.
ADDENDUM
Specifically, using regexrep() you should be able to find the keyword of interest followed by the value and then replace the value token with the desired in a single command for each of the two elements desired.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by