Matlab mat to libsvm format (strange fprint behaviour) - matlab

I'm trying to write my own implementation of mat2libsvm format converter(I don't want to use original function because it want double mat for input, but I working with images and have uint8 matrices).
So here is example that I don't understand:
a= zeros(2,256);
a(1,256)=1;
formatSpec = '%i:%d ';
row= a(1,:);id=find(row);fprintf(formatSpec,[id ; row(id)]);
>256:1
row= uint8(a(1,:));id=find(row);fprintf(formatSpec,[id ; row(id)]);
>255:1
why it cuts off to 255? anyway id in 1st and 2nd examples is double.

In the second line you are concatenating an uint8 with an double, which casts both to uint8. Minimal example:
[256;uint8(1)]
To solve this, use fprintf with multiple input arguments:
fprintf(formatSpec,id , row(id));

Related

MATLAB: In this function, why won't it let me set the precision after the decimal point for reading data from a text file?

I have written this function to read data from a text file and store it in a matrix, where the columns are variables and rows samples. Format spec can be used to define the precision.
function A = readGivenData(dataFileName,noOfVariables,formatSpec)
fileID = fopen(dataFileName,'r');
[A,~] = fscanf(fileID,formatSpec,[noOfVariables inf]);
fclose(fileID);
A = A';
end
One of the variables has five places after the decimal point (the default with this function seems to be only 4) but using this function and inputting '%.5f' as the formatSpec simply returns a 0x0 empty char array. Why doesn't this work?

collapse cell array to text matlab

it's a basic question I guess (but I'm new to Matlab), but given:
>> class(motifIndexAfterThresholds)
ans =
'double'
with :
16
8037
14340
21091
27903
34082
as the contents of that variable
I hoped to print to the same line on the matlab console the contents of that variable and some other output:
fprintf('With threshold set to %d, %d motifs found at positions %f.\n',threshold,length(motifIndexAfterThresholds), motifIndexAfterThresholds);
When I do this however, I'm getting more than one line of output:
With threshold set to 800, 6 motifs found at positions 16.000000.
With threshold set to 8037, 14340 motifs found at positions 21091.000000.
With threshold set to 27903, 34082 motifs found at positions
Can someone share the method for collapsing this double array to a single line of text that I can display on the Matlab console please?
What you need is the num2str function that is builtin MATLAB. Modify your code as below:
strThresholds = num2str(motifIndexAfterThresholds.', '%f, '); % Transpose used here since you need to make sure that motifIndexAfterThresholds is a row vector
fprintf('With threshold set to %d, %d motifs found at positions %s.\n',threshold,length(motifIndexAfterThresholds), strThresholds);
The num2str function will convert your vector to a string with the specified format. So for your given example,
strThresholds = '16.000000, 8037.000000, 14340.000000, 21091.000000, 27903.000000, 34082.000000,'
You could definitely edit the format string used in the num2str funcion to suit your needs. I would suggest using %d since you have integers in your vector

MATLAB how to store Value in Variable without scientific/exponential notation?

I've got a Matrix, let's say x, that contains seven values in exponential notation. Next i want to write this Variable to a textfuile, but without the exponential but with a decimal notation.
I tried str2num(num2str(exportdata, '%15.4f')); and fprintf but this only works for displayed data but not for storage as far as I know.
You can use fprinf to print directly to the file. For example:
v = [173524132746354.21542, 987678898521232.32547]
fid = fopen('file.txt','w')
fprintf(fid, '%0.2f, %0.2f', v)
fclose(fid)

How to import non-comma/tab separated ASCII numerical data and render into vector (in matlab)

I want to import 100,000 digits of pi into matlab and manipulate it as a vector. I've copied and pasted these digits from here and saved them in a text file. I'm now having a lot of trouble importing these digits and rendering them as a vector.
I've found this function which calculates the digits within matlab. However, even with this I'm having trouble turning the output into a vector which I could then, for example, plot. (Plus it's rather slow for 100,000 digits, at least on my computer.)
Use textscan for that:
fid = fopen('100000.txt','r');
PiT = textscan(fid,'%c',Inf);
fclose(fid);
PiT is a cell array, so convert it to a vector of chars:
PiT = cell2mat(PiT(1));
Now, you want a vector of int, but you have to discard the decimal period to use the standard function:
Pi = cell2mat(textscan(PiT([1,3:end]),'%1d', Inf));
Note: if you delete (manually) the period, you can do that all in once:
fid = fopen('100000.txt','r');
Pi = cell2mat(textscan(fid,'%1d',Inf));
fclose(fid);
EDIT
Here is another solution, using fscanf, as textscan may not return a cell-type result.
Read the file with fscanf:
fid = fopen('100000.txt','r');
Pi = fscanf(fid,'%c');
fclose(fid);
Then take only the digits and convert the string as digits:
Pi = int32(Pi((Pi>='0')&(Pi<='9')))-int32('0');
Function int32 may be replaced by other conversion functions (e.g. double).
It can be done in one line.
When you modify your text file to just include the post-comma digits 14... you can use fscanf to get the vector you want.
pi = fscanf( fopen('pi.txt'), '%1d' ); fclose all
or without modifying the file:
pi = str2double( regexp( fscanf( fopen('pi.txt') ,'%1c'), '[0-9]', 'match'));

Matlab rgb2hsv dimensions

I am reading in the matlab documentation that rgb2hsv will return an m-by-n-by-3 image array, yet when I call it, I get a 1-by-3 vector. Am I misunderstanding something?
Here is an sample code:
image_hsv = rgb2hsv('filepath')
and as output
image_hsv =
0.7108 0.3696 92.0000
You cannot call rgb2hsv on a filepath - it must be called on a MATLAB image matrix. Try:
image_rgb = imread('filepath'); % load the image array to MATLAB workspace
image_hsv = rgb2hsv(image_rgb); % convert this array to hsv
You can see these matrices with:
>> whos image* % display all variables whose name begins with 'image'
Name Size Bytes Class Attributes
image_hsv 480x640x3 7372800 double
image_rgb 480x640x3 921600 uint8
What your original code was doing was converting your filepath string to ascii numbers, taking the first three values of this array as RGB values and converting these to HSV.
NOTE: This example highlights dangers with MATLAB's weak typing system, where data types are silently converted from one type to another. Also maybe a lack of correct input checking to the rgb2hsv function.