Extremely simple question:
how can I format my code to be nicely readable. Example:
A = (B+C+D+E+F+G+H+I+J+K...)
and let's say it is so long that I have to scroll for ages to later see what I wrote.
If however I press enter to separate the line like this:
A = (B+C+D+E
+F+G+H+I...)
matlab reports error
Thanks
Use ... at the line break. It is a line continuation.
Use ... to split lines:
Instead of a = x + y + z, you can use:
a = x ...
+ y...
+ z
You're looking for "line continuation".
http://www.mathworks.com/help/techdoc/matlab_env/f0-5789.html#f0-5857
Related
Trying to print the following range of labels in a figure:
aux = {'ca155.mat','ca154.mat','ca159.mat','ca146.mat','ca148.mat','ca004.mat'};
But I need it upper case and without the extension, so I use
text(0,0,upper(sprintf([aux{i},'\b\b\b\b'])));
In the command window I get the correct output such as for i=1, i.e. CA155. However the text function on a figure doesn't work and produces:
CA155.MAT[][][][]
Except instead of brackets there are closed rectangles (I couldn't copy the character).
How can I fix this?
When processing your text, you did not delete the extension, you inserted backspaces. Here some insights for demonstration:
>> x=upper(sprintf([aux{i},'\b\b\b\b']))
x =
'CA155'
>> size(x)
ans =
1 13
>> x(1:9)
ans =
'CA155.MAT'
>> x(1:10)
ans =
'CA155.MA'
The first 9 characters are still there but the following backspaces delete them when working in a command window. Looks like text does not support it, and backspaces are definitely not the way to go.
Use fileparts instead:
>> [filepath,name,ext]=fileparts(aux{i})
filepath =
0×0 empty char array
name =
'ca155'
ext =
'.mat'
I want to use fscanf for reading a text file containing 4 rows with an unknown number of columns. The newline is represented by two consecutive spaces.
It was suggested that I pass : as the sizeA parameter but it doesn't work.
How can I read in my data?
update: The file format is
String1 String2 String3
10 20 30
a b c
1 2 3
I have to fill 4 arrays, one for each row.
See if this will work for your application.
fid1=fopen('test.txt');
i=1;
check=0;
while check~=1
str=fscanf(fid1,'%s',1);
if strcmp(str,'')~=1;
string(i)={str};
end
i=i+1;
check=strcmp(str,'');
end
fclose(fid1);
X=reshape(string,[],4);
ar1=X(:,1)
ar2=X(:,2)
ar3=X(:,3)
ar4=X(:,4)
Once you have 'ar1','ar2','ar3','ar4' you can parse them however you want.
I have found a solution, i don't know if it is the only one but it works fine:
A=fscanf(fid,'%[^\n] *\n')
B=sscanf(A,'%c ')
Z=fscanf(fid,'%[^\n] *\n')
C=sscanf(Z,'%d')
....
You could use
rawText = getl(fid);
lines = regexp(thisLine,' ','split);
tokens = {};
for ix = 1:numel(lines)
tokens{end+1} = regexp(lines{ix},' ','split'};
end
This will give you a cell array of strings having the row and column shape or your original data.
To read an arbitrary line of text then break it up according the the formating information you have available. My example uses a single space character.
This uses regular expressions to define the separator. Regular expressions powerful but too complex to describe here. See the MATLAB help for regexp and regular expressions.
I have a folder contained several files in it
loc1.txt loc2.txt .... loc10.txt
I want to use them in matlab this is my code :
for i=1:10
myFile =['E:\dis\locs\loc' i '.txt'];
b= importdata(myFile);
but it does not work and output is like this :
'E:\dis\locs\loc .txt'
is there any body help me here?
You need to convert i into characters.
myFile =['E:\dis\locs\loc' num2str(i) '.txt'];
Nemesis' answer using num2str is correct. Another possibility is to use sprintf:
myFile = sprintf('E:\dis\locs\loc%d.txt', i);
The interface is less intuitive if you have never seen it before but it is also very convenient when you have zero-padded numbers, like loc0001.txt, loc0002.txt, etc. In this case just replace %d by %04d like this:
myFile = sprintf('E:\dis\locs\loc%04d.txt', i);
I'm a neuroscience/biomedical engineering major struggling with this whole MATLAB programming ordeal and so far, this website is the best teacher available to me right now. I am currently having trouble with one of my HW problems. What I need to do is take a phrase, find a specific word in it, then take a specific letter in it and increase that letter by the number indicated. In other words:
phrase = 'this homework is so hard'
word = 'so'
letter = 'o'
factor = 5
which should give me 'This homework is sooooo hard'
I got rid of my main error, though I really don;t know how. I exited MATLAB, then got back into it. Lo and behold, it magically worked.
function[out1] = textStretch(phrase, word, letter, stretch)
searchword= strfind(phrase, word);
searchletter strfind(hotdog, letter); %Looks for the letter in the word
add = (letter+stretch) %I was hoping this would take the letter and add to it, but that's not what it does
replace= strrep(phrase, word, add) %This would theoretically take the phrase, find the word and put in the new letter
out1 = replace
According to the teacher, the ones() function might be useful, and I have to concatenate strings, but if I can just find it in the string and replace it, why do I need to concatenate?
Since this is homework I won't write the whole thing out for you but you were on the right track with strfind.
a = strfind(phrase, word);
b = strfind(word, letter);
What does phrase(1:a) return? What does phrase(a+b:end) return?
Making some assumptions about why your teacher wants you to use ones:
What does num = double('o') return?
What does char(num) return? How about char([num num])?
You can concatenate strings like this:
out = [phrase(1:a),'ooooo',phrase(a+b:end)];
So all you really need to focus on is how to get a string which is letter repeated factor times.
If you wanted to use strrep instead you would need to give it the full word you are searching for and a copy of that word with the repeated letters in:
new_phrase = strrep(phrase, 'so', 'sooooo');
Again, the issue is how to get the 'sooooo' string.
See if this works for you -
phrase_split = regexp(phrase,'\s','Split'); %// Split into words as cells
wordr = cellstr(strrep(word,letter,letter(:,ones(1,factor))));%// Stretched word
phrase_split(strcmp(phrase_split,word)) = wordr;%//Put stretched word into place
out = strjoin(phrase_split) %// %// Output as the string cells joined together
Note: strjoin needs a recent MATLAB version, which if unavailable could be obtained from here.
Or you can just use a hack obtained from the m-file itself -
out = [repmat(sprintf(['%s', ' '], phrase_split{1:end-1}), ...
1, ~isscalar(phrase_split)), sprintf('%s', phrase_split{end})]
Sample run -
phrase =
this homework is so hard and so boring
word =
so
letter =
o
factor =
5
out =
this homework is sooooo hard and sooooo boring
So, just wrap the code into a function wrapper like this -
function out = textStretch(phrase, word, letter, factor)
Homework molded edit:
phrase = 'this homework is seriously hard'
word = 'seriously'
letter = 'r'
stretch = 6
out = phrase
stretched_word = letter(:,ones(1,stretch))
hotdog = strfind(phrase, word)
hotdog_st = strfind(word,letter)
start_ind = hotdog+hotdog_st-1
out(start_ind+stretch:end+stretch-1) = out(start_ind+1:end)
out(hotdog+hotdog_st-1:hotdog+hotdog_st-1+stretch-1) = stretched_word
Output -
out =
this homework is serrrrrriously hard
As again, use this syntax to convert to function -
function out = textStretch(phrase, word, letter, stretch)
Well Jessica first of all this is WRONG, but I am not here to give you the solution. Could you please just use it this way? This surely run.
function main_script()
phrase = 'this homework is so hard';
word = 'so';
letter = 'o';
factor = 5;
[flirty] = textStretchNEW(phrase, word, letter, factor)
end
function [flirty] = textStretchNEW(phrase, word, letter, stretch)
hotdog = strfind(phrase, word);
colddog = strfind(hotdog, letter);
add = letter + stretch;
hug = strrep(phrase, word, add);
flirty = hug
end
I think it's a simple question. I want:
a = 1.154648126486416;
to become:
a = 1.154;
and not:
a = 1.15000000000;
How do I do that without using format('bank').
You could do this:
a = floor(a*1000)/1000;
Building on #gnovice's answer, you can format the output as a string to get rid of the extra zeros. See the sprintf documentation for all the formatting options.
str=sprintf('The result is %1.3f.',a);
disp(str)
will show "The result is 1.154." in the command prompt. Or write the string to file, etc., etc.
a = 1.154648126486416;
% desired precision
b = -3;
% your answer
ans = floor(a*10^(-b))/(10^(-b));
The answer is 1.1540
this is good if you don't care about the rest of digits but if you do care then you just have to simply change "floor" to "round".
ans = round(a*10^(-b))/(10^(-b));
The answer is 1.1550