how to make a function whose first input argument is a cell vector (row or column) of strings and whose second input argument is a string.

4 views (last 30 days)
Hi every one; I am going to attempt that question: Write a function called censor whose first input argument is a cell vector (row or column) of strings and whose second input argument is a string. It removes each element of the cell vector whose string is either identical to the second input argument or contains the second argument as a substring. The resulting cell vector is returned as the only output argument. For example, suppose each element of the first argument refers to one line of the first verse of the US national anthem and the second argument is 'the'. Then the function would return a cell vector with a single element containing the string: 'Oh, say does that star-spangled banner yet wave'.
I have made that program:
function mystr=censor(vec,str)
for i=1:length(vec)
if vec(i)==str
vec(i)=str;
else
mystr=vec;
end
end
end
but that error:
Feedback: Your program made an error for argument(s) { 'Oh, say can you see by the dawn''s early light', 'What so proudly we hailed at the twilight''s last gleaming?', 'Whose broad stripes and bright stars thru the perilous fight,', 'O''er the ramparts we watched were so gallantly streaming?', 'And the rocket''s red glare, the bombs bursting in air,', 'Gave proof through the night that our flag was still there.', 'Oh, say does that star-spangled banner yet wave', 'O''er the land of the free and the home of the brave?' }, 'the'
Your solution is _not_ correct.
Kindly check my program whether i am doing correct or wrong. Please guide me about correction? Thanks in advance for assistance...
  10 Comments
Guillaume
Guillaume on 4 Jun 2015
Edited: Guillaume on 4 Jun 2015
The error with the above code would be
Error using strfind
Not enough input arguments.
which is a different error that you would have got with any other code you've posted. You would have seen that immediately if you'd tested it in matlab.
I'd recommend you do not use cellfun as its syntax is probably too complex for a beginner. If you do, note that the first argument must be a function handle and that the cell array inputs must all have the same size.
Use a loop instead.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 7 Jun 2015
contains_str = false(1, length(vec));
for K = 1 : length(vec)
if ~isempty(strfind(vec{K}, str))
contains_str(K) = true;
end
end
mystr = {};
for K = 1 : length(vec)
if ~contains_str(K)
mystr(end+1) = vec(K);
end
end

More Answers (2)

Image Analyst
Image Analyst on 7 Jun 2015
I would use John D'Errico's allwords http://www.mathworks.com/matlabcentral/fileexchange/27184-allwords in combination with ismember() or setdiff().
  1 Comment
Muhammad Usman Saleem
Muhammad Usman Saleem on 7 Jun 2015
@image thanks for contributions...... But the after getting hints from a lot of people i getting confuse what i have to do in this question...

Sign in to comment.


dpb
dpb on 7 Jun 2015
Against better judgment but since Walter's already posted one solution, for the original problem description the suggested approach is-
>> s={ 'Oh, say can you see by the dawn''s early light', 'What so proudly we hailed at the twilight''s last gleaming?', 'Whose broad stripes and bright stars thru the perilous fight,', 'O''er the ramparts we watched were so gallantly streaming?', 'And the rocket''s red glare, the bombs bursting in air,', 'Gave proof through the night that our flag was still there.', 'Oh, say does that star-spangled banner yet wave', 'O''er the land of the free and the home of the brave?' };
>> s(cellfun(@isempty,strfind(s,'the')))
ans =
'Oh, say does that star-spangled banner yet wave'
>>
As noted, it's likely cellfun is more advanced than your course at the moment, so work backwards and see how to make the above without it using a loop instead.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!