-3

I have a list of numbers in a column vector. In those numbers i have an alphabet M which appears at random intervals..

this link How to delete zero components in a vector in Matlab? display how to remove the Zero, i tried to adapt how to remove M, but in vain.!

How do I replace this M by 0? I tried this code but to no avail.!

I called all my sample data N.

N=[4.6 6.7 4.1 3.1 M 2.6]

N(N==M) = []; i also tried this code sample=N(N~=M);

My real data is loaded from a text file:

filename='x.txt'; 
N=importdata(filename)
Community
  • 1
  • 1
Zay
  • 115
  • 2
  • 6
  • Could you post sample data, please. – kkuilla Oct 17 '14 at 08:17
  • is 0 an acceptable entry? Are negative numbers? and what about NaN? – The Minion Oct 17 '14 at 08:44
  • hi. no negative number, and yeah i want Zero to replace the M.. thnaks – Zay Oct 17 '14 at 08:44
  • 1
    What is the "alphabet" `M`? A number? A row vector of numbers? Please give a more specific example defining `M` – Luis Mendo Oct 17 '14 at 08:47
  • Then you could define yourself a string containing all letters ['A':'Z','a':z'] and check with i think `strcomp()` (not sure I am writing without matlab right now). To check if any of your elements is part of that array as well if true replace them – The Minion Oct 17 '14 at 08:47
  • and most important in what kind of data do you save those elements? definetely not double because M is not an allowed entry. is N a cell array, a struct or a string? – The Minion Oct 17 '14 at 08:48
  • hi. @TheMinion, M is just this an alplabeth M which has been randomly generated in my data.. It represent Missing data from my apapratus which measure temperature...N is a bunch of data found in my text file..see this.. x.txt is my text file..filename='x.txt'; N=importdata(filename).....I want to remove all M frm my data.. – Zay Oct 17 '14 at 08:57
  • matlab doesn't accept a letter inside a double array. datatypes must match. The only known exceptions as far as I know are `NaN`,`inf` and variable names of double-variables. – The Minion Oct 17 '14 at 09:20
  • 1
    I personally have my doubts that `importdata` is able to correctly import your text file. Check the size of your data `N` versus what you expect it to be. – nkjt Oct 17 '14 at 09:31
  • Sorry, you have got many working answers that does not help you. You need to give better information about: error message, datatype (from the output variable you get when you run your code, with the `class` function) – patrik Oct 17 '14 at 17:49

3 Answers3

3

The problem is that your M items are never being imported by importdata in the first place!

importdata is the wrong function to use for complex data. If you put this in x.txt:

4.6
6.7
4.1
3.1
M
2.6

Then the output of N=importdata(filename) is simply the first four values. It can't handle the M. You should notice this, because the size of N should be smaller than the number of values in your file.

Instead, use textscan, telling it that M is an invalid item and should be replaced with 0:

fid = fopen('x.text');
N = textscan(fid,'%f','treatAsEmpty',{'M'},'EmptyValue',0);
fclose(fid);
N{1}

ans =

    4.6000
    6.7000
    4.1000
    3.1000
         0
    2.6000

Additional note: it's probably a bad idea to put 0 in where you mean this value was bad, because it will affect the results you get from other functions. I would set EmptyValue to NaN instead.

nkjt
  • 7,825
  • 9
  • 22
  • 28
  • you are right.. but now i have to run my weibull on the N pd=fitdist(N,'Weibull').. it does not work when i run the code you put.. it only work when i import data.. – Zay Oct 17 '14 at 09:48
  • 1
    `textscan` produces a cell array. Do `N = N{1};` and it will probably work. Then, please go back and read the MATLAB help on data types because you are going to get yourself into more trouble if you don't take the time to understand the basics. – nkjt Oct 17 '14 at 09:51
  • it does not work..'fid = fopen('x.txt'); N = textscan(fid,'%f','treatAsEmpty',{'M'},'EmptyValue',0); fclose(fid); N=N{1}; pd=fitdist(N,'Weibull')' – Zay Oct 17 '14 at 09:56
  • +1 @Zay It does work. I've run patriks and nkjt's answer and they both work. Unfortunately, it's the information you provided that is incorrect. Your question wasn't very well defined. It didn't describe what you were trying to do accurately and there were important information missing. Don't take this the wrong way (and don't be discouraged) but I think you need to go and do your homework first. [How To Ask](http://stackoverflow.com/help/how-to-ask) is a good read. – kkuilla Oct 17 '14 at 10:26
  • Please post a new question regarding that. Don't forget to accept **and upvote** your preferred answer. – kkuilla Oct 17 '14 at 10:32
  • See edit: you probably shouldn't be putting in zeros if M means "valid data was not recorded", really. – nkjt Oct 17 '14 at 11:07
2

Assumes that you do not really have numbers, but numbers as a string. This means that you can use the function strrep.

try:

A = ['1 2 3 4 M 6'];
strrep(A,'M', '0');
patrik
  • 4,506
  • 6
  • 24
  • 48
  • Warning: Inputs must be character arrays or cell arrays of strings. it does not work.. – Zay Oct 17 '14 at 08:49
  • filename='x.txt'; N=importdata(filename); strrep(N,'M', '0'); – Zay Oct 17 '14 at 08:50
  • x is a text file which contains a bunch of numbers and those M at random places... – Zay Oct 17 '14 at 08:50
  • Ok, but what is the type of your data. You can try to run `class(N)` – patrik Oct 17 '14 at 08:51
  • a sample 4.6 6.7 4.1 0 3.1 2.6 3.1 1 1 2.6 M – Zay Oct 17 '14 at 08:53
  • Ok but what is the **datatype**?It is obviously not a string since my answer did not work. It is neither a single, double or int since M is not a valid double, single or int. I need to know the datatype in order to help. Run `class(N)` and add the output of this to your question. – patrik Oct 17 '14 at 09:01
  • 1
    the only way I can imagine this error occurring is if **x.txt is an empty text file or contains only numbers**... this would cause that error as N would be an double (empty or not), other wise N should either be a cell array of strings or a string (char array), depedning on if the txt file had multiple lines or not – RTL Oct 17 '14 at 09:05
  • >> class(N) ans = double – Zay Oct 17 '14 at 09:06
  • @Zay Have you actually checked the values of `N`? If I do N=[4.6 6.7 4.1 3.1 M 2.6] I don't get an `M` in it. I get N =[ 4.6000 6.7000 4.1000 3.1000 2.6000 2.6000]. – kkuilla Oct 17 '14 at 09:10
  • i have given only a sample of N.. i have more than 100 thousand data in N, and the alphabet M appears around 5000 times.. – Zay Oct 17 '14 at 09:13
  • How do I remove an unwanted alphabet from a bunch of numbers in matlab? – Zay Oct 17 '14 at 09:15
  • @zay, @patriks answer works. It's your sample data that is wrong. I bet you copied `N` straight from the text file and not from the Matlab workspace. – kkuilla Oct 17 '14 at 09:30
1

Hey unfortunately you haven't provided what kind of datatype you have in N. As given by OP double doesn't make sense, because M is not a valid double value as far as I know.

So I assume that you have a cell array containing doubles or strings placed in cells. If so this code works:

N={ 1 2 2 42 5 12 'M' 'm' 123}
alphabet=['A':'Z','a':'z'];

for k=1:numel(N)
    if ismember(N{k},alphabet)
        N{k}=0;
    end
end
display(N)

resulting in following console output: input

N = 

    [1]    [2]    [2]    [42]    [5]    [12]    'M'    'm'    [123]

output

N = 

    [1]    [2]    [2]    [42]    [5]    [12]    [0]    [0]    [123]

You can change what the replacement is in the if statement.

The code can be modified to fit a string as input:

N=['1 2 2 42 5 12 M m 123']
alphabet=['A':'Z','a':'z'];

for k=1:numel(N)
    if ismember(N(k),alphabet)
        N(k)='0';
    end
end
display(N)
The Minion
  • 1,164
  • 7
  • 16
  • hi.. a code as such should work..sample=N(N~=M); ie i simply want to erase M, from my sample N... and replace it by Zero – Zay Oct 17 '14 at 09:22
  • I tried this and the `for` loop gives me an error: `error: Invalid call to lookup.` Bear in mind that I tried it on GNU Octave Version 3.8.1 – kkuilla Oct 17 '14 at 10:31
  • @kkuilla hm strange i tried both codes prior to posting them here and they worked for me. Not sure if it is because of your System or what went wrong. – The Minion Oct 17 '14 at 10:47