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

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 comentario

if r= 3: 3 or r = 2:2 no error also, error come when for loop run more than 1 time

Iniciar sesión para comentar.

Respuestas (1)

dpb
dpb el 15 de Jul. de 2017

0 votos

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 comentarios

Muhammad Saad Naeem
Muhammad Saad Naeem el 15 de Jul. de 2017
Editada: dpb el 15 de Jul. de 2017
* C:\Users\student\Documents\naeem_masood\Matlab + ltspice\Nmos.asc \n
M1 Vds Vin 0 N001 nm l=10u w=170u
VDS Vds 0 0.1
VGS Vin 0 1
VBS N001 0 0
.model NMOS NMOS
.model PMOS PMOS
.lib C:\PROGRA~2\LTC\LTSPIC~1\lib\cmp\standard.mos
.MODEL nm NMOS(LEVEL=1 VT0=1.758 KP=129.8u GAMMA=1.995 lambda=11.8m)
.dc VGS 0 5 0.25 VBS 0 -3 -1
.backanno
.end
The above text is not just i have to read but change the values of GAMMA and lambda so that's why this way of reading is not look good any way i'll try your method........
dpb
dpb el 15 de Jul. de 2017
Editada: dpb el 15 de Jul. de 2017
Tell us/show an example of what it is you're trying to accomplish--I'd still bet it can be done quite a bit more simply if we had enough info to know the task definition.
Actually I want to run .cir file of lt-spice from matlab aur plot VI diagram which i have done but now want change parameters of circuit from matlab for which i need to read .cir file ............ find the required parameters e.g. lambda or GAMMA, replace it with user given value and then re-write the .cir file
Walter Roberson
Walter Roberson el 15 de Jul. de 2017
One thing to note is that starting from R2017a, by default importdata() returns string objects instead of cell array of string. That can lead to code suddenly stopping working.
dpb
dpb el 15 de Jul. de 2017
"... 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 el 15 de Jul. de 2017
Editada: dpb el 17 de Jul. de 2017
"...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.

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 15 de Jul. de 2017

Editada:

dpb
el 17 de Jul. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by