Error using load Must be a string scalar or character vector

thanks a lot for your answer I now understand my mistake but after this when I am trying to write the rain flow script in order to reduce a spectrum of varying stress there is this error which is showing"Error using load Must be a string scalar or character vector.I am also attaching the script thanks in advance for your help.

5 Comments

load is primarily used for adding variables from a file into a work space. I am not quite sure what you are trying to achieve by the command -
s1=load(i);
@Prateek Srivastava: What do you expect these to do?:
s1=load(i);
s2=load(i+1);
s3=load(i+2);
s4=load(i+3);
Inventing syntaxes is a very inefficient way of writing code. You would be much better off reading the documentation and using syntaxes that are actually supported by MATLAB.
I am trying to extract the first four values from my signal and than calculate the values of S1,S2,S3,S4 and than apply the rainflow conditions that is abs(s2-s1)>=abs(s2-s3),abs(s3-s4)>=abs(s2-s3); if this is true than we could extract that point otherwise we move on.
Is the signal in the .txt file? If so can you please attach that as well?
@Prateek: The code
abs(s2-s1)>=abs(s2-s3),abs(s3-s4)>=abs(s2-s3)
% ^
does not make sense. The comma means, that both parts are evaluated, but independent from each other. A simplified code:
x = 1;
y = 2;
if x == 1, y == 7
Now the condition of the IF command is "x == 1". Afterwards y is compared to 7, but this does not concern the IF in any way. Maybe you want:
if abs(s2-s1)>=abs(s2-s3) & abs(s3-s4)>=abs(s2-s3)
% or
if abs(s2-s1)>=abs(s2-s3) | abs(s3-s4)>=abs(s2-s3)
But
if abs(s2-s1)>=abs(s2-s3) , abs(s3-s4)>=abs(s2-s3)
is equivalent to:
if abs(s2-s1)>=abs(s2-s3)

Sign in to comment.

Answers (2)

I am trying to extract the first four values from my signal
The symbol "load" was not defined as a variable. Then the command load() is used, but this cannot be meant. I cannot know which variable is meant, perhaps B or t? Where do you expect "load" to be defined?
Other hints:
if abs(s2-s1)>=abs(s2-s3),abs(s3-s4)>=abs(s2-s3);
What is the purpose of the part behind the comma? Perhaps you want:
if abs(s2-s1)>=abs(s2-s3) & abs(s3-s4)>=abs(s2-s3)
"sign" is a built-in function. Shadowing it by a local variable can cause serious confusion if you want to use the function later on. Avoid to use the names of important Matlab functions as local variables. See:
clear('sign')
sign(2)
sign = rand(1, 10);
sign(2)

5 Comments

but what about this error I am getting ,I have changed the symbol from signal to stress. Trial>> fatiguelife Error using load Must be a string scalar or character vector.
Error in fatiguelife (line 14) s1=load(i);
@Prateek Srivastava: Seriously? Did you read my answer? Let me repeat the essential part:
"load" is not a defined variable. Then Matlab use it as command load(), which expects a file name as first input. And such file names are provided as string or char vector, as the error message tells you.
Think twice: What do you expect the line "s1 = load(i)" to do? Do you want to copy the i.th element of the array "load" to the variable s1? Then why do you assume that "load" is an existing variable? It is not.
Use the debugger to check your code. Type this in the command window:
dbstop if error
or set a breakpoint in the failing line. Then run the code again until it stops at the error. Now check the variables, which are used in this line:
which load
You will see, that "load" is the function to load MAT files. Then "load(i)" must fail.
so which command can I use to pick the first value from the table which I got after clearing the signal from the first part of the script as S1 and than S2,S3,and S4. I am attaching the stress signal with time so once you run the script you would come to know with these set of values.
yeah I got your point that load is a function that takes file name string so what can I use to copy the ith element of the array "load" to the variable s1?
@Prateek Srivastava: There is no variable called "load" in your posted code. The error message means, that there is no such variable at all. In consequence, you cannot obtain the i.th element of this variable, because this variable does not exist at all.
Maybe you want the data of A or B?

Sign in to comment.

You imported the data and stored the stress values etc. inside a struct A. s1 should get the value from the structure instead of using load command.
A=importdata('rain.txt');
This will create a struct A with A.data containing your required values.
>> A.data
ans =
1 -10
2 10
3 0
4 30
5 -20
6 0
7 -10
8 20
9 0
If you want to get the first stress value - access it by indexing through 'A.data'
s1 = A.data(1,2)
s1 =
-10
>> s2 = A.data(2,2)
s2 =
10
and so on... Hope this helps.

7 Comments

The data from the 'rain.txt' was used to plot the graph between stress and time and than when I found the peaks and valley I sorted the signal out so that I could clear the signal and get one table which you could see in the script is named as stress_PV.Now I would like to extract the first value from this table stress_PV and the next one till I finish .So that is what I want to do. If you could just run the script you would come to know which data I am talking about. Thanks a lot for your guidance.
  • Firstly, there is no stress_PV in the script you provided. Are you referring to sign_PV?
  • If so, then sign_PV is not a table but a column vector which contains 9 elements. You want to access the element not via a 'load' command but using vector indexing.
>> sign_PV
sign_PV =
-10
10
0
30
-20
0
-10
20
0
while i<=(length(sign_PV))
s1=sign_PV(i);
s2=sign_PV(i+1);
s3=sign_PV(i+2);
s4=sign_PV(i+3);
...
And then proceed with the computation you were talking about while taking care of the index limits and boundary conditions.
We would usually call that a column vector (a vector in the form of a column) rather than a row vector (which would be a vector in the form of a row)
Oops my bad. I've corrected that mistake.
yes you are right it was named as sign_PV my mistake,actually I changed the name from sign_PV to stress_PV because it was conflicting with the Matlab command. I did what you said but still there is one error it is showing: Subscript indices must either be real positive integers or logicals.
Error in fatiguelife (line 14) s1=stress_PV(i); I am also attaching the script please find the attachment.
Your code does not assign any value to i before it uses i
Use a 'for' loop, initialize the value of i and then proceed to assignment. Or you could initialize and update the value of i accordingly in the 'while' loop.
Thank you Walter Roberson!

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!