String handling in matlab - arrays

I am working on this code which is to copy the sentences stored in one array to another.
'text1' is the array which stores all my sentences and C1 is the array into which the sentences have to be copied.
text1 is a 1x8 array with text1(1,1) containing the first sentence, text1(1,2) with the second sentence and so on.
The following is the code that I have written to copy the contents from text1 to C1:
for i=1:vr
if(Track(i)<0)
text1{1,i};
C1(1,j)=text1(1,i)
j=j+1;
end
end
Can somebody help me?Thanks in advance.

Since you haven't included examples of neither text1, Track nor vr I can't test anything. But your assigning C1 incorrectly if its a cell array. Use C1{1,i} = text{1,i}instead.
But, if you want to copy everything in text1 into a new cell array, with exactly the same contents C1 = text1; will do that.

If Track is an array, you should be able to do it follows (using logical indexing):
C1 = text1(Track < 0);
Or something similar to that, depending on exact structure of your data.

Have you initialized the cell C1 and j?
j = 1;
C1 = {};
for i=1:length(text1)
if( Track(i)<0 )
text1{1,i};
C1(1,j)=text1(1,i)
j=j+1;
end
end

Related

storing the longest string after strsplit

I am trying to store the longest resultant string after using the function strsplit unable to do so
eg: I have input strings such as
'R.DQDEGNFRRFPTNAVSMSADENSPFDLSNEDGAVYQRD.L'or
'L.TSNKDEEQRELLKAISNLLD'
I need store the string only between the dots (.)
If there is no dot then I want the entire string.
Each string may have zero, one or two dots.
part of the code which I am using:
for i=1:700
x=regexprep(txt(i,1), '\([^\(\)]*\)','');
y=(strsplit(char(x),'.'));
for j=1:3
yValues(1,j)=y{1,j};
end
end
But the string yValues is not storing the value of y, instead showing the following error:
Assignment has more non-singleton rhs dimensions than non-singleton subscripts
What am I doing wrong and are there any suggestions on how to fix it?
The issue is that y is a cell array and each element contains an entire string and it therefore can't be assigned to a single element in a normal array yvalues(1,j).
You need yvalues to be a cell array and then you can assign into it just fine.
yValues{j} = y{j};
Or more simply
% Outside of your loop
yValues = cell(1,3);
% Then inside of your loop
yValues(j) = y(j);
Alternately, if you just want the longest output of strsplit, you can just do something like this.
% Split the string
parts = strsplit(mystring, '.');
% Find the length of each piece and figure out which piece was the longest
[~, ind] = max(cellfun(#numel, parts));
% Grab just the longest part
longest = parts{ind};

Array fill not working as expected

I am trying to fill an array of length 300 with the same value using VB here is my code:
Dim counter As Integer
If (value = True) Then
For counter = LBound(arrLowThresholds) To UBound(arrLowThresholds) Step 1
arrLowThresholds(counter) = LOG_TEST_LOW_THRESHOLD
arrHighThresholds(counter) = LOG_TEST_HIGH_THRESHOLD
Next
End If
The problem with this is that only the first element of the arrays is being filled.
Note that some of the variables are declared elsewhere that is the reason that the declaration is not visible.
arrLowThresholds and arrHighThresholds are the arrays while LOG_TEST_LOW_THRESHOLD and LOG_TEST_HIGH_THRESHOLD are the variables
I solved the problem by making a local array to use with the for loop. Then I copied the contents to the array I am actually using.

How to split sentences in an array

I have a string s which stores a very long sentence and I want to copy the content of s to an array C with each cell storing a sentence each. The following is my code which is not giving me any output, but the dimension of the cell:
while(i<6)
C(i)=s;
end
This is how I get as output when I print C:
C=
[1x76 char]
Can somebody please help me.
Another job for strsplit:
>> sentences = 'This is the first one. Then here is a second. Yet another here.';
>> C = strsplit(sentences,'. ')
C =
'This is the first one' 'Then here is a second' 'Yet another here.'
We are specifying a period followed by a space as the delimiter. Change this as needed.
Suppose Long string is:
longString = "This is first cell. This is second cell. this is third cell".
Now since . is delimiter here means it is acting as separator for sentences. so you can loop through longString character wise and whenever you encounter a . you just increase Array index count and keep storing in this Array index until you find another .
here is sudo code:
array[];
index = 0;
loop through(longString) character wise
{
if(currentChar equals to '.')
{
index++;
}
else
{
array[index] = currentChanracter;
}
}

MATLAB string to number error

I have a dozens of arrays with different array names and I would like to do some mathematical calculations in to for loop array by array. I srucked in calling these array into for loop. Is there anybody can help me with this problem? text1 array contains array names. My "s" struct has all these arrays with the same name content of text1 array.
text1=['s.CustomerArray.DistanceDriven','s.CustomerArray.TimeDriven'];
for i=1:3
parameter=str2num(text1(i));
k=size(parameter,2);
a=100;
y=zeros(a,k);
end
After this part my some other calculations should start using "parameter"
Regards,
Eren
I think you are doing several things wrong, here are some pointers.
Rather than listing them manually, consider looping over the fieldnames which can be obtained automatically.
If you are looping over strings, make sure to use a cell array with , rather than a matrix.
If you have a constant, declare it outside the loop, rather than inside the loop. This won't break the code but just makes for obsolete evaluations.
If you want to store results obtained inside a loop, make sure to add an index to the variable that you loop over.
That being said, here is a guess at what you are trying to do:
f = fieldnames(s.CustomerArray);
y = cell(numel(f),1);
parameter = NaN(numel(f),1);
for t = 1:numel(f)
parameter(t) = s.CustomerArray.(f{t});
y{t} = zeros(100,numel(f{t}));
end

Creating arrays in Matlab

I want to store user inputs in an array, but when a person enters a new number, the previous input gets replaced. How can I create such an array in Matlab such that I can store all inputs without replacement? I am a beginner so bear with me
Thanks
You simply need to copy the contents of the input buffer into a data struct that won't be overwritten.
Cell arrays are good for that (see the userInputs variable below) . Without better knowledge of your code, I'm guessing the user input is stored in a variable named buffer. Here's how I would do it:
% a new buffer comes in
userInputs{iInput} = buffer;
iInput = iInput + 1;
% keep looking for more inputs
Good luck!
If you want a numeric matrix here is an example:
n = 2; %# number of rows
m = 3; %# number of columns
out = zeros(n,m); %# the output
k = 1; %# counter
while k <= n*m
x = input('Enter a number or Enter to stop: ');
if isempty(x)
break
else
out(k)=x;
end
k=k+1;
end
disp(xx)

Resources