[Linux] Chmod

I encountered the ‘access restriction’ problem when I tried to host a local webpage on to my department’s server.

The problem was that all my images inside the .html file had broken link. (where the annoying broken image icon: broken_logo shown everywhere across your website) I double checked my image directory in the .html file and there was nothing wrong with it.

Later I figured out it was the permission of my folder and files problem. It turned out that you CANNOT (at least I’m not sure how) modify the permissions or inspect what the permission of non-users are by right clicking the file/folder and checking ‘Get Info‘ on Mac. You need to use the terminal for that. And it’s all about the Chmod tricks.

Chmod stands for ‘change mode’. It modifies the access permissions to file system objects. The basic syntax is:

chmod [options] file

where ‘options’ is a 10-bit binary 9876543210; if denote the rightmost digit as index 0, then 0, 1, 2 bits control permission of other users, 3, 4, 5 bits control permission of the group, and 6, 7, 8 bits control permission of the user. 10th bit is optional. Each of the three bits represent ‘r’ for read, ‘w’ for write and ‘x’ for execute. An illustration is shown below:


Basically, if you want to allow for ‘r’ for user, then you go to the three bits that control user permission (6, 7, 8 bits) and set 8th bit to one since it controls ‘r’.

Assume we want to change permission of a file to 1) user being able to read, write and execute, 2) group and other users being able to read only. Then we set 2, 5 to one (‘r’ for other users and the group) and 8, 7, 6 to all ones (for user to read, write and execute). Then the 10 bit binary is: 0111100100, which is equivalent as writing in 744 (111–7, 100–4, 100–4).

Instead of using numbers to denote the permission mode, we can also use ‘r’, ‘w’, ‘x’, etc. directly. ‘u’ for user, ‘g’ for group and ‘o’ for others. For example, ‘u+x’ means user is able to execute, ‘o+r’ means others can read.

chmod u+rwx filename

is the same as

chmod 700 filename

If you then want to inspect on what files have what permission, go to your file directory and type:

ls -l filename

Then you are able to figure our why certain images/files are not able to open, and what permissions you have over those files.


[MATLAB] Retrieve data from .fig files

In MATLAB, when you saved an image into a .fig file and then want to retrieve the data from it, you can do as follows:

First, get a handle to the input .fig image by either doing

h = gcf;  % handle to the current displayed image.

or do (relate .fig format to .mat format):

h = hgload('display.fig');   % also give you the handle to the figure file

Now h is the handle to your image file. When you close the window of the displayed image, h is deleted.

Next step is to go down the hierarchy from the figure handle –> axes object –> image object (contains the data you need)

axesObjs = get(h, 'Children');       % get the axes object from the handle
imageObj = get(axesobj, 'Children')  % get the image object from the axes object

Now that you have your image object, you are able to retrieve all source of information of your .fig image file. Within this ‘imageObj’ object, some major information:

XData: the x-dimension size of your image
YData: the y-dimension size of your image
CData: this is the matrix of your image pixel intensities

There are other properties stored in the image object such as transparency, etc. But most of the time we care about the size and content of our image file. Last step is to retrieve the info you need using the ‘get’ function:

image = get(imageObj, 'CData'); % give you the image data

The second argument could be any property contained in the image object. (e.g. XData, YData, CData, etc.)