loading and saving with eval using sprintf

25 ビュー (過去 30 日間)
npns15
npns15 2011 年 4 月 8 日
コメント済み: Ashraf Rayed 2020 年 5 月 12 日
Hi, i am solving a non-linear ode problem and getting errors on running the code at following points:
Initially at
fname=sprintf('ts32/f%d',findex);
eval(['load' fname]);
and at end of code at
s=sprintf('ts32/f%dt%d',findex,tindex);
eval(['save' s 't x output load_torque load_force height mu force tanforce theta dt w j k l m n'])
As i am new to matlab can someone please guide as to if there is any error in the syntax
Thanks in advance

採用された回答

the cyclist
the cyclist 2011 年 4 月 8 日
It's hard to know if there is a syntax error, without knowing what the variables "findex" and "tindex" are.
I have two suggestions:
  1. Good idea: Rather than sending those concatenated strings to "eval", breakpoint your code, see what those string look like. Copy and paste those commands by hand into the command window, and see if they work. (Looks like you might be missing whitespace around the load and save keywords.)
  2. Better idea: Don't use "eval" at all. Use the functional form of the load and save commands (see the help file for syntax), that allow you to input strings for file names and variables.
If these work out for you, please accept this answer. If not, then next time you comment please tell us what "findex" and "tindex" are, and also give us the exact text of the error message(s) you are getting.
  3 件のコメント
npns15
npns15 2011 年 4 月 11 日
編集済み: Walter Roberson 2020 年 5 月 12 日
Hi Sir,
Tried using functional form of load and save commands but it didn't work out..maybe some mistake on my part.
I tried by using whitespace but the error i am getting now is :
??? Input argument "x" is undefined.
Error in ==> eomlinear at 13
w=defl*[cos(4*theta) sin(4*theta)]*[x(1);x(2)];
Error in ==> motorlinear at 64
[t,x]=ode23(eomlinear,[t0 tf],x0,tol);
The complete code is in 3 matlab files as follows (deflinear.m,motorlinear.m and eomlinear.m):
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%deflinear.m defines all definitions
%%%Define all matrices and constants
global Amp freq tau omega theta dt
global r1 r2 rt height defl Ac Aratio kr mu Ir Mr Calpha Cz
global zeta M Minv C K Theta Cp
Amp=50; %Amplitude of applied voltage
freq=7151.09;% Drive frequency at resonance in Hz
tau=1/freq;
omega=2*pi*freq;
%Define area of integration
numpoints=180;
t1=0;
t2=pi/2;
dt=(t2-t1)/numpoints;
theta=linspace(t1,(t2-dt),numpoints)';
%Radial location of edges of annular plate
a=2.5e-3;
b=25e-3;
%Data on teeth
nt=32; %nos. of teeth
r1=0.9*b;
r2=b;
rt=(r1+r2)/2;
height=0.001651;
%%Assumed modes
coef=[0.577589 0.452799];
defl1=coef(1)*((rt-a)/(b-a))^2;
defl2=coef(2)*((rt-a)/(b-a))^3;
defl=defl1 + defl2;
%%Stator
zeta=0.005;% Assumed damping ratio
%Define Mass, Damping, Stifness, E-Mcoupling and Capacitance matrices
mon=0.00387494;
moff=0.00271152;
M=[mon 0;0 moff]; %Mass matrix
Minv=inv(M);
C=[4*pi*freq*zeta*M(1,1) 0;0 4*pi*freq*zeta*M(2,2)]; % Damping matrix
kon=7.82292e6;
K=[kon 0;0 kon]; %Stiffness matrix
ton=-3.62623e-10;
Theta=[ton 0;0 ton]; %E-M Coupling matrix
con=7.90987e-8;
Cp=[con 0;0 con]; %Capacitance Matrix
mu=0.3; % Dynamic Coefficient of friction for the interface
%Rotor
Ir=9.6e-6; % Rotor inertia in kg-sqm
Mr=3.1e-2; %Assumed rotor mass in kg
Calpha=2e-5; %Rotor spin damping in N-m-s
Cz=150; %Rotor vertical damping in N-s/m
%%%%%%%%%%%%%motorlinear.m is the main file%%%%%%%%%%%%%%
clear all;
%Read all definitions from 'definitions.m'
global Amp freq tau omega theta dt
global r1 r2 rt height defl Ac Aratio kr mu Ir Mr Calpha Cz
global zeta M Minv C K Theta Cp
deflinear;
%%Define variable terms:
global t x load_force load_torque
global A B tol tlast wr wf
global force tanforce normwork1 normwork2 tanwork1 tanwork2
global w vel i ii j k l m n output
%%%%System dynamics
%State vector[p1 p2 p1dot p2dot alpha alphadot wf wfdot]'
I=eye(2);
O=0*I;
A=[O I O O;
-Minv*K -Minv*C O O;
0 0 0 0 0 1 0 0;
0 0 0 0 0 -Calpha/Ir 0 0;
0 0 0 0 0 0 0 1;
0 0 0 0 0 0 0 -Cz/Mr];
B=[O O;Minv O;O [0 0;1/Ir 0];O [0 0;0 1/Mr]];
%% Solve the ODE
load_force=5;%Assume applied force is 5 N
load_torque=0;%Assume applied torque is 0 Nm
% IC for modal amplitude
amp=2.5e-6;
ampdot=amp*omega;
phaseloss=90*pi/180;
amp1=amp*cos(phaseloss);
amp2=amp*sin(phaseloss);
ampdot1=-ampdot*sin(phaseloss);
ampdot2=ampdot*cos(phaseloss);
%IC for rotor
alpha=0;
alphadot=0;
%IC for flexure height
wf=height+amp-load_force/2e6;
wfdot=0;
% Time
t0=0;
tlast=t0;
tf=200*tau; %Length of time simulation
%Initial conditions
x0=[amp1 amp2 ampdot1 ampdot2 alpha alphadot wf wfdot]';
%ODE Routine
tol=1.0e-3;
[t,x]=ode23(@eomlinear,[t0 tf],x0,tol);
exit;
%%%%%%%%%%%%%%%eomlinear.m is the function file for ode and calculating the non-linear terms%%%%%%%%%%%%%%%
function xdot=eomlinear(t,x)
global Amp freq tau omega
global r1 r2 rt height defl Ac Aratio kr mu Ir Mr Calpha Cz
global zeta M Minv C K Theta Cp
global load_force load_torque
global A B theta dt tlast wr wf
global force tanforce normwork1 normwork2 tanwork1 tanwork2
global w vel i ii j k l m n output
%Displacement and Velcoity profiles
w=defl*[cos(4*theta) sin(4*theta)]*[x(1);x(2)];
vel=-4/rt*height*defl*[sin(4*theta) -cos(4*theta)]*[x(3);x(4)];
kr=7.2e9;
%Rotor speed and flexure height
wr=x(6);
vr=rt*wr;
wf=x(7);
%Contact Pressure
j=find(w>=(wf-height));%Find points of contact
force=zeros(size(theta));
if length(j)>0
one1=ones(length(j),1);
%A force per unit theta
force(j)=rt*(r2-r1)*kr*(w(j)+one1*(height - wf));
normwork1=-4*dt*sum(defl*cos(4*theta).*force);
normwork2=-4*dt*sum(defl*sin(4*theta).*force);
else
normwork1=0;
normwork2=0;
end;
%Relative Velocity
region=0.0;
%Friction region (dynamic friction)
k=find(vel > (1 + region)*vr);
l=find(vel<(1 - region)*vr);
%Static friction
m=find((vel < (1 + region)*vr) & (vel > vr));
n=find((vel < vr) & (vel > (1 - region)*vr));
tanforce=zeros(size(theta));
if((length(k) > 0)|(length(l) > 0))
tanforce(k)=mu*force(k);
tanforce(l)=-mu*force(l);
tanforce(m)=2*mu*force(m);
tanforce(n)=-2*mu*force(n);
tanwork1=+4*dt*sum(4*height/rt*defl*sin(4*theta).*tanforce);
tanwork2=-4*dt*sum(4*height/rt*defl*cos(4*theta).*tanforce);
else
tanwork1=0;
tanwork2=0;
end;
%Interface force and torque
normforce=4*dt*sum(force);
torque=4*rt*dt*sum(tanforce);
%%%Dynamics of system
% Drive voltage
v1=Amp*cos(omega*t);
v2=Amp*sin(omega*t);
%Forcing terms
fv1=Theta(1,1)*v1;
fn1=normwork1;
ft1=tanwork1;
fv2=Theta(2,2)*v2;
fn2=normwork2;
ft2=tanwork2;
f1=fv1+fn1+ft1;
f2=fv2+fn2+ft2;
f3=torque-load_torque;
f4=normforce-load_force;
f=[f1;f2;f3;f4];
xdot=A*x + B*f;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
I am solving this non-linear problem for the first part of my work and this code is based on some earlier references i found. I need to reproduce this results for the first part of my work.
Any help will be appreciated,
Thanks and regards
Walter Roberson
Walter Roberson 2011 年 4 月 11 日
In your ode call, use @eomlinear rather than eomlinear .

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

その他の回答 (2 件)

Robert Cumming
Robert Cumming 2011 年 4 月 8 日
why dont you just do:
fname = fullfile ( directory, filename )
load ( fname )
then use similar at the end for saving:
fname = fullfile ( dir, savename )
save ( fname, 'variable' );
  1 件のコメント
npns15
npns15 2011 年 4 月 9 日
Thanks for the prompt reply sir, will try this method also
Regards

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


Ashraf Rayed
Ashraf Rayed 2020 年 5 月 12 日
編集済み: Walter Roberson 2020 年 5 月 12 日
i have this code, i have to check the leaf area of some pictures in a folder and then i have save the area of those leaves by sequence in an excel sheet. Can you plkease help to sort out the problem?
D = 'F:\MATLAB\R2018a\bin\rayed mat lab\experimental';
S = dir(fullfile(D,'*.jpg'));
for k = 1:numel(S)
F = fullfile(D,S(k).name);
I = imread(F);
ih=rgb2hsv(I);
ih1=ih(:,:,1);
binary=ih1>0.6;
leafArea = sum(binary(:))
subplot(2, 2, 2);
imshow(binary, []);
message = sprintf('The leaf area = %d pixels.\n pixels = %.1f%%',...
leafArea, defectArea/leafArea*100);
uiwait(msgbox(message))
end
  5 件のコメント
Ashraf Rayed
Ashraf Rayed 2020 年 5 月 12 日
It is not working to save all the data in the sheet even it is not to create any sheet also
Ashraf Rayed
Ashraf Rayed 2020 年 5 月 12 日
showing this messege====
Undefined function or variable 'leafFractions'.
Error in Untitled (line 20)
T = table({S.name}.', leafArea(:), leafFractions(:), ('VariableNames'), {'FileName', 'Area', 'Fraction'});

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

カテゴリ

Help Center および File ExchangeModel Type and Other Transformations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by