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)

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.