0

I'm writting a code to convert integer to unary using the matlab
i read integers from file and write the unary in other file

the problem the file i write in is always empty (nothing is Written )

this is my attempt
the test.txt contain 1 2 3 4

file_read=fopen('d://test.txt');
file_write=fopen('d://write.txt','w');
line=fgets(file_read);
code2=[];
num=str2num(line);
len=length(num);
for (i=1:len)
    number=num(i);
z=zeros(1,number-1)
code=[z 1]
end
fwrite(file_write,code)
fclose(file_write)

Edited by osfar: n/a

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by pyTony
0

Without knowing matlab, it looks like you are not splitting the line for individual numbers, usually str2num style function work on single number. Could be different in matlab though..

Googled it out (http://www.mathworks.com/matlabcentral/fileexchange/4615-split-delimiter-separated-strings-into-a-matrix) and it does seem to be able to handle multiple numbers as long as you tell it the delimeter, if it is not ','. You have delimeter ' ' so that should be reason for your problem.

Edited by pyTony: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.