Unexpected MATLAB expression? (trying to create a function) [closed] - matlab

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
load('matrix.mat');
userInput = input('input a value from 1-5')
DayReport = sum(matrix(:,end 2);==userInput)
I am trying to retrieve the number of rows in column 2 of the loaded matrix that corresponds with the userInput. However, when I try to run the code, it says that there is an error in the third line (simply, "Unexpected MATLAB expression"). Any ideas as to why this is?
EDIT: I found a solution, turns out i don't need the "end" or the semicolon within the sum function.
load('matrix.mat');
userInput = input('input a value from 1-5')
DayReport = sum(matrix(:,2)==userInput)

Remove the semi-colon and the end statement in the last line of code. My guess is that you want to access the second column of matrix, and with that it's just matrix(:,2).
Also, I suspect that you copied and pasted the code from somewhere. That's generally bad programming practice because where you copied the code from may work in that situation, but if you try and bring it into your current context, it may be slightly different than what you're actually doing and can result in errors.
See this good discussion on Programmers Stack Exchange on why you should avoid this all together: https://softwareengineering.stackexchange.com/questions/87696/is-copy-paste-programming-bad

Related

Perl Text Balance extract bracketed returns null no error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am trying to debug a Perl Script, which uses the Text Balance extract bracketed function on some text that is enclosed in curly braces.
Unfortunately, the function returns nothing, not in the extracted or the remainder and there is error.
Is there a way where I can find out there reason for its failure? Is it possible for it to throw an exception saying for example, not able to extract because of the data structure. At the moment without knowing why, I can't really solve the problem.
See the Diagnostics section of the doc.
In addition, on failure in any context, the $# variable is set. Accessing $#->{error} returns one of the error diagnostics listed below. Accessing $#->{pos} returns the offset into the original string at which the error was detected (although not necessarily where it occurred!) Printing $# directly produces the error message, with the offset appended. On success, the $# variable is guaranteed to be undef.

Find and replace multiple string in MATLAB [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have a1 in the following form in MATLAB.
a1{1,1} = {'x1','x2','x3'}
a1{1,2} = {'x1','x2','x3','x4'}
a1{1,3} = {'x4'}
I need to replace
'x1' with 'Text1'
'x2' with 'Text2'
'x3' with 'Text3'
'x4' with 'Text4'
I have tried a couple of things with no success and I am a bit lost, does anyone have any ideas.
Thanks
Regular Expressions might be a way to go with this
a1{1,1} = {'x1','x2','x3','x4'};
a1{1,1} = regexprep(a1{1,1},'x([0-9])','text$1');
Will result in a1{1,1} containing
{'text1'} {'text2'} {'text3'} {'text4'}
This will work for any single digit numerical value (0-9) that is prefixed with the letter x.

How to use the matlab function tar? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am now want to compress a file name File1.txt which is in the current folder. When I try to use the function tar like:
tar('File1.tgz','File1.txt');
Some error happen.
Undefined variable "File1" or function "File1.tgz".
Error in tar (line 1)
tar(File1.tgz,'File1.txt');
Is it any incorrect part of this?
You forgot to put ''.
Try:
tar('File1.tgz','File1.txt');
In my MatlabR2013a it works without problems.
Matlab example:
%Tar all files in the current directory to the file backup.tgz
tar('backup.tgz','.');

Is this a vaild variable name? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For those that have experience in PowerShell, is this considered to be a valid variable name?
Code:
$[?#+-stuff] is a valid variable name in PowerShell.
It's a valid? Yes, if you wrap it inside curly brackets. This is required because of the special characters.
${[?#+-stuff]} = "foo"
Is it a good name? No. You should keep it simple..
If you made an effort to research it you would have figured it out yourself.
Check of the help documentation the #mjolinor suggested. Especially the part called:
VARIABLE NAMES THAT INCLUDE SPECIAL CHARACTERS
We're all here to help each other, so please show us some respect and behave like a grown-up.

Comments are getting executed when I run the Perl Script [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Some of my perl statements are executed even after commenting. I tried all the delimiter #, /*..*/, //
Does anyone face the same issue or can anyone help me how to solve this issue?
Your question appears to be "How do I create a comment in Perl?"
perlsyn says thusly:
Text from a "#" character until the end of the line is a comment, and is ignored. Exceptions include "#" inside a string or regular expression.
For example,
print "apple\n"; # Keeps the doctor away.
Keep in mind that comments can only be used where whitespace is expected. For example, the following does not contain any comments since the # is part of the string literal.
print "apple # Keeps the doctor away.
orange
";